CodeIgniter Step By Step Tutorial: We have set database and create table for CodeIgniter practice. Now, we learn how to show data in CodeIgniter. As we know, CodeIgniter use MVC pattern. We will use Model for retrieve data from database.
First, build a model. Create a file named “books_model.php” within CodeIgniter/CodeIgniter\system\application\models. Enter following code:
<?php
class Books_model extends Model{
function Books_model(){
parent::Model();
}
function books_getall(){
$this->load->database();
$query = $this->db->get('books');
return $query->result();
}
}
?>
Next, we make a view. Create a file named “books_view.php” within CodeIgniter\system\application\views. Enter folowing code:
<?php echo "<pre>"; print_r($query); echo "<pre>"; ?>
we want to debug that the data come from our model so that we use print_r() to see the data in array format. Latter we will see this clearly.
Build controller. Create a file named “books.php” within CodeIgniter\system\application\controllers. Enter following code:
<?php
class Books extends Controller{
function Books(){
parent::Controller();
}
function get_All(){
$this->load->model('books_model');
$data['query']=
$this->books_model->books_getall();
$this->load->view('books_view',$data);
}
}
?>
Now, try to point your browser to http://localhost/CodeIgniter/index.php/Books/get_All

So if we put this code in view
<?php
foreach($query as $row){
echo $row->isbn;
echo $row->title;
echo $row->description;
echo $row->description;
}
?>
Then we see this out put

At last if we want to show this data in table then we can write this code
<?php
echo "<table border=1>";
echo "<tr>";
echo "<td>Id</td>";
echo "<td>Isbn</td>";
echo "<td>title</td>";
echo "<td>description</td>";
echo "</tr>";
foreach($query as $row){
echo "<tr>";
echo "<td>".$row->id."</td>";
echo "<td>".$row->isbn."</td>";
echo "<td>".$row->title."</td>";
echo "<td>".$row->description."</td>";
echo "</tr>";
}
echo "</table>";
?>
And we will see the out put like



nice……. keep it up mama!!
thank
Nice Tutorial for freebies working on CodeIgniter.
ThanX
When i use display data code its generate error like
query is undefind variable in view page. why this happen?
A.A
Thanks for this tutorial.
Very easy and nice for new on Codeigniter
nice tutorial
It is giving me these errors
Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\htdocs\ci\system\codeigniter\Common.php on line 130
Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\htdocs\ci\system\codeigniter\Common.php on line 136
is this is on your latest code ignitor
Nice code for lerning and working on CodeIgniter.
nice code … helped me a lot.