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

Friday, June 8, 2012

Introduction to Codeigniter


Read The Indonesian Version
CodeIgniter is open source PHPFramework. It's free to use and manipulate as you wish. CodeIgniter uses MVC (Model View Controller) model programming. MVC is a programming concept where logic and layout are separated, so that the programmers and designers can do their job in focus. Codeigniter can download here.



Picture MVC model programming

Model Used to process the database in this case is the CRUD (Create, Read, Update, Delete)
Controller Class and / or function that will be called on the browser address bar and use to get query results from the model and then display it on the view
View Here is the WEB design area, here they are working enhance the data display query results, by adding the CSS and JS

For the begining let us give instance, how to create "Hello PHP" with controller and view.
Create a php file and rename it with c_hello.php, copy this following code
 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_hello extends CI_Controller{
            
   function __construct(){
                
     parent:: __construct();
                
   }
            
   function index(){ 
     $data['hello'] = "Hello PHP";
     $this->load->view('v_hello',$data);
   }
}
?>
  
Second create view and named it with v_hello.php, copy this following code
 
<?= $hello; ?>
 

** GOOD LUCK **