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

Retrieving user-related data from a MySQL table - 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

Since my plan consists basically of demonstrating how to build a basic PHP application, capable of paging a few database rows, I’m going to use the same sample “users” MySQL table that was created in the previous article. As you’ll recall, the table was populated with data about some fictional users. Its structure looked like this:



Now that you’re familiar with the above MySQL table, it’s time to build a model class that permits us to access its rows in a painless fashion. So take a look at the following “User” model class, which accomplishes this task. Here it is:

class Users extends Model{

function Users(){

// call the Model constructor

parent::Model();

// load database class and connect to MySQL

$this->load->database();

}

function getAllUsers(){

$query=$this->db->get('users');

if($query->num_rows()>0){

// return result set as an associative array

return $query->result_array();

}

}

function getUsersWhere($field,$param){

$this->db->where($field,$param);

$query=$this->db->get('users');

// return result set as an associative array

return $query->result_array();

}

// get 5 rows at a time

function getUsers($row){

$query=$this->db->get('users',5,$row);

if($query->num_rows()>0){

// return result set as an associative array

return $query->result_array();

}

}

// get total number of users

function getNumUsers(){

return $this->db->count_all('users');

}

}

If you read the preceding tutorial of the series, it’s possible that you find the above model class pretty familiar. As you can see, it presents a group of methods that can be used for fetching both single and multiple rows from the pertinent “users” MySQL table, as well for counting the number of records contained within a given result set.

Also, it’s worthwhile to clarify again that this model implements all of its methods thanks to the functionality of the database class included with Code Igniter, which is loaded by the corresponding constructor.

However, you should pay close attention to the signature of a brand new method, called “getUsers().” It permits the program to fetch five rows at time.  I’m going to take advantage of its functionality to build a simple, yet effective, paging system.

Now that you hopefully grasped how the previous model class does its thing, please save it to the system/application/models/ folder and jump forward to see how to define the other building block of this MySQL-driven application. In this case, I’m talking about the corresponding controller class, which not only will be capable of embedding database contents into a view file, but will also implement the aforementioned paging mechanism.

To learn how this controller class will be constructed, please click on the link that appears below and keep reading.



 
 
>>> 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: