Friday, June 29, 2012

Create View on CodeIgniter

Read The Indonesian Version
This time the authors would like to share a little knowledge about using MVC model in CodeIgniter, for this example we try to discuss how to Create View on CodeIgniter. Before starting make sure you have the required below
  • Codeigniter versi 2.1.0 can download here
  • Create database with name "latihan"
For the first create Table "user" :
 
id (int 4) autoincrement

username (varchar 30)

access (varchar 10) 
 
and insert into table, min 2 record.

Next create file "view.php" put on application->controller
 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class View extends CI_Controller{

     function __construct(){

          parent::__construct();

          $this->load->model("crud");
     }

     function index(){

         $data['title']="View User";
         $data['header']="View User";
         $data['query']=$this->crud->view();

         $this->load->view('view',$data);

     }
?>
 
Then create file "view.php", at this time place on application->view
 
<table>

     <thead>
         <tr>
               <th>No</th>
               <th>User</th>
               <th>Access</th>
        </tr>
     </thead>

     <tbody>
         <?php
               $no = 1;
               foreach($query as $row){
         ?> 
         <tr>
               <td><?php echo $no;?></td>
               <td><?php echo $row->username;?></td>
               <td><?php echo $row->access;?></td>   
         </tr>
         <?php $no ++;      } 
         ?> 
     </tbody>

</table>

 
For the last, create file "crud.php" place it on application->model
 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Crud extends CI_Model{

    function view_user(){
        #Table Properties
        $tabel = "user";

        #Query
        $query = $this->db->select("*")->from($tabel)->get();
        if ($query->num_rows() > 0) {
           return $query->result();
        } else {
           return array();
        }
    }
}
?> 
type on address bar www(dot)example(dot)com/index(dot)php?/view

2 comments

Offshore Software Development Company July 4, 2012 at 12:57 AM

i was looking for hire codeigniter programmers and find your post and i must say this is quite useful for me thanks for sharing.

Unknown July 8, 2012 at 4:24 AM

glad to read your comment

Post a Comment