CodeIgniter: Showing Simple All Data

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
codeigniter - show  print  data from view

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
ci-view data with foreach
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
ci- show data in view in table

 

the coder

10 Comments on "CodeIgniter: Showing Simple All Data"

  1. saif says:

    nice……. keep it up mama!!

  2. admin says:

    thank

  3. Fawaz says:

    Nice Tutorial for freebies working on CodeIgniter.
    ThanX

  4. nehal says:

    When i use display data code its generate error like
    query is undefind variable in view page. why this happen?

  5. Muhammad Khalid Ch. says:

    A.A
    Thanks for this tutorial.
    Very easy and nice for new on Codeigniter

  6. khakan says:

    nice tutorial

  7. Dileep says:

    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

  8. is this is on your latest code ignitor

  9. baljit says:

    Nice code for lerning and working on CodeIgniter.

  10. ajay says:

    nice code … helped me a lot.

Got something to say? Go for it!

*

 
More in beginners (3 of 10 articles)