Home arrow PHP arrow Page 3 - Paginating Database Records with the Code Igniter PHP Framework

Paging database records with Code Igniter - PHP

In the previous tutorials in this nine-part series, we built a simple application with the Code Igniter PHP framework. In this article, the third part of the series, we're going to add some important functionality to this application by using the pager class included with Code Igniter.

TABLE OF CONTENTS:
  1. Paginating Database Records with the Code Igniter PHP Framework
  2. Retrieving user-related data from a MySQL table
  3. Paging database records with Code Igniter
  4. Completing the development of the sample MySQL-driven application
By: Alejandro Gervasio
Rating: starstarstarstarstar / 21
August 27, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: