PHP
  Home arrow PHP arrow Page 3 - Paginating Database Records with the Code Igniter PHP Framework
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Paginating Database Records with the Code Igniter PHP Framework
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2008-08-27


    Table of Contents:
  • Paginating Database Records with the Code Igniter PHP Framework
  • Retrieving user-related data from a MySQL table
  • Paging database records with Code Igniter
  • Completing the development of the sample MySQL-driven application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Paginating Database Records with the Code Igniter PHP Framework - Paging database records with Code Igniter
    ( Page 3 of 4 )

    Indeed, the philosophy that drives Code Igniter allows us to simplify the development of complex applications. And paginating database records certainly isn’t an exception, since the framework includes a solid pager class, which can be easily customized to suit the requirement of a huge number of projects.

    In this particular case, where it’s necessary to create a user controller class that uses the methods of the model coded previously, the pager class of Code Igniter fits perfectly into this schema. Below I built a brand new controller that exploits the functionality of this pager class to paginate rows fetched from the prior “users” MySQL table.

    Here’s the code for this class:


    class Users extends Controller{

    function Users(){

    // load controller parent

    parent::Controller();

    // load 'Users' model

    $this->load->model('Users');

    }

    function display($row=0){

    // load pagination library

    $this->load->library('pagination');

    // set pagination parameters

    $config['base_url']='http://127.0.0.1/codeigniter/index.php/users/display/';

    $config['total_rows']=$this->Users->getNumUsers();

    $config['per_page']='5';

    $this->pagination->initialize($config);

    // store data for being displayed on view file

    $data['users']=$this->Users->getUsers($row);

    $data['title']='Displaying user data';

    $data['header']='User List';

    $data['links']=$this->pagination->create_links();

    // load 'testview' view

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

    }

    }

    As illustrated above, the “User” controller class looks pretty similar to the one built in the previous tutorial. However, there's an important difference that I’d like to point out here, since it implements a new method called “display().”

    As you can see, the method in question first includes, via the corresponding loader object, the pagination class. Then, it initializes some of its parameters, such as the base URL of the paging links, the total number of rows that will be spawned, and finally the number of records that will be displayed per page.

    Once the pagination class has been correctly set up, it’s used for creating the paging links via its “create_links()” method. Lastly, the “display()” method retrieves the paginated rows from the corresponding “users” MySQL table, which are directly embedded along with the pertinent links into a view file. And before I forget, please save the user controller class to the Code Igniter system/application/controllers/ folder.

    However, if you’re anything like me, you wonder how the view file will be created, right? Thus, in the last section of this tutorial I’m going to show you how to define this file, in this way finishing the development of this MySQL-driven application aimed at demonstrating how to paginate database rows with Code Igniter.

    What are you waiting for? Jump forward and read the next section!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT