Page Navigation using CodeIgniter Pagination

In our previous tutorial called “Getting Started with CodeIgniter” I discuss on how to install and configure CodeIgniter. This time I will teach you on how to create a simple “Page Navigation”. This is just a comparison of my previous code found at PHP Page Navigation. You’ll learn the difference between the standard PHP and using a CodeIgniter framework.

Now let us configure the CodeIgniter.

  1. Open autoload.php under Config folder using your PHP Editor.
  2. Open config.php under Config folder.
  3. Open database.php under Config folder.
  4. Open routes.php under Config folder.
  5. Now create a file called pagination.php under controllers folder and paste the following code:
    1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    2.  
    3. class Pagination extends CI_Controller
    4. {
    5.         public function __construct() {
    6.                 parent:: __construct();
    7.                
    8.                 $this->load->helper("url");
    9.                 $this->load->model("Countries_Model");
    10.                 $this->load->library("pagination");
    11.         }
    12.  
    13.         public function index() {
    14.                 $config["base_url"] = base_url() . "pagination";
    15.                 $config["total_rows"] = $this->Countries_Model->record_count();
    16.                 $config["per_page"] = 10;
    17.                 $config["uri_segment"] = 2;
    18.  
    19.                 $this->pagination->initialize($config);
    20.  
    21.                 $page = ($this->uri->segment(2))? $this->uri->segment(2) : 0;
    22.                 $data["results"] = $this->Countries_Model
    23.                         ->get_countries($config["per_page"], $page);
    24.                 $data["links"] = $this->pagination->create_links();
    25.  
    26.                 $this->load->view("pagination", $data);
    27.         }
    28. }      
  6. Create a file called countries_model.php under models folder and paste the following code:
    1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    2.  
    3. class Countries_Model extends CI_Model
    4. {
    5.         public function __construct() {
    6.                 parent::__construct();
    7.         }
    8.  
    9.         public function record_count() {
    10.                 return $this->db->count_all("tblcountries");
    11.         }
    12.  
    13.         public function get_countries($limit, $start) {
    14.                 $this->db->limit($limit, $start);
    15.                 $query = $this->db->get("tblcountries");
    16.  
    17.                 if ($query->num_rows() > 0) {
    18.                         foreach ($query->result() as $row) {
    19.                                 $data[] = $row;
    20.                         }
    21.                         return $data;
    22.                 }
    23.                 return false;
    24.    }
    25. }      
  7. Lastly, create a file called pagination.php under views folder and paste the following code:
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4. <meta charset="utf-8">
    5. <title>Pagination with CodeIgniter</title>
    6. </head>
    7. <body>
    8. <div id="container">
    9.         <h1 style="text-align:center">Page Navigation in PHP</h1>
    10.         <table width="400" border="1" align="center">
    11.                 <tr>
    12.                         <td width="100" bgcolor="#CCCCCC"><p>Country ID</p></td>
    13.                         <td width="300" bgcolor="#CCCCCC">Country</td>
    14.                 </tr>
    15.                 <h1 style="text-align:center">Countries</h1>
    16.                         <div id="body">
    17.                
    18.                 <?php
    19.                 foreach($results as $data) {
    20.                 ?>
    21.                 <tr>
    22.                         <td><?php echo $data->CountryID ?></td>
    23.                         <td><?php echo $data->Country ?></td>
    24.                 </tr>
    25.                 <?php
    26.                 }
    27.                 ?>
    28.         </table>
    29.         <div style="text-align:center"><?php echo $links; ?></div>
    30. </div>
    31. </div>
    32. </body>
    33. </html>

Now, open your browser and type:
http://localhost/CI/

Note: this assumes that you extract the CodeIgniter framework under www folder and named it as “CI”.

Comments

its good if i download and used to it

sir i just want to ask, if codeigniter works on all php editors such as dreamweaver, notepad ++ etc.?

In reply to by espionage (not verified)

Yes...

how to apply css to links appeared in pagination?

how to insert radio buttons in a table?

why is it undefined variable: links, results, in filename:pages/pagination.php and invalid argument supplied for foreach()

An uncaught Exception was encountered Type: RuntimeException Message: Unable to locate the model you have specified: Countries_model Filename: /opt/lampp/htdocs/CI/system/core/Loader.php Line Number: 344

This is really a good start up for life because of this website also. Because we know that Stackoverflow helps us to findout best resource. Also this website inspire a developer to develop more and more skill about programming. Because of this .Website .Thank you so much for create this website.

Add new comment