Monday, 9 March 2015

How to display data from a table using CodeIgniter?

(1)write code for class Emp_model & save as  Emp_model.php


<?php

class Emp_model extends CI_Model

{

  function emp_getall()

  {
       $this->load->database();
       $query=$this->db->get('items');//items is a table in the database
       return $query->result();
  }

}


?>


(2)wrtie code for class emp_viewall & save as emp_viewall.php:


<?php

foreach($query as $row)
{
 
  print $row->name;
 
 
  print "<br>";
 
}

?>

(3)write code for class emp & save as emp.php:

<?php
class emp extends CI_Controller
{
 
  function GetAll()
  {
     $this->load->model('emp_model');

       $data['query']=$this->emp_model->emp_getall();

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

?>





(4)Run code  by typing  url on browser:

"http://localhost/ci/index.php/emp/getall"

No comments:

Post a Comment