Home arrow PHP arrow Page 2 - Previous or Next? Paginating Records with PHP - Part 2

Taking the procedural approach: the friendly "paginateRecords()" function - PHP

In the first article in this series, we learned how to do simple pagination of records taken from a text file. In this article, we will look at pagination for a larger group of records, using an Objected Oriented rather than a procedural approach to creating the application.

TABLE OF CONTENTS:
  1. Previous or Next? Paginating Records with PHP - Part 2
  2. Taking the procedural approach: the friendly "paginateRecords()" function
  3. Taking the OOP approach: defining the "Pager" PHP class
  4. Defining the "displayRecords()" method
  5. Source code ahead: completing the "displayRecords()" method
  6. Pager class implementation
By: Alejandro Gervasio
Rating: starstarstarstarstar / 10
May 03, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the first article, we defined a simple text file for storing information, where each line is considered a single record, and each column is a different field. In programming environments, this is known as a flat database. We went as far as setting up a rather basic function to paginate data retrieved from a text file. The function's source code was originally defined as shown below:

function paginateRecords($dataFile,$page,$numRecs=5){

$output='';

// validate data file

(file_exists($dataFile))?$data=array_reverse(file
($dataFile)):die('Data file not valid.');

// validate number of records per page

(is_int($numRecs)&&$numRecs>0)?$numRecs=$numRecs:die
('Invalid number of records '.$numRecs);

// calculate total of records

$numPages=ceil(count($data)/$numRecs);

// validate page pointer

if(!preg_match("/^\d{1,2}$/",$page)
||$page<1||$page>$numPages){

$page=1;

}

// retrieve records from flat file

$data=array_slice($data,($page-1)*$numRecs,$numRecs);

// append records to output

foreach($data as $row){

$columns=explode('|',$row);

foreach($columns as $column){

$output.=$column.'&nbsp;';

}

$output.='<br />';

}

// create previous link

if($page>1){

$output.='<a href="'.$_SERVER['PHP_SELF'].'?page='.
($page-1).'">&lt;&lt;Previous</a>&nbsp;';

}

// create intermediate links

for($i=1;$i<=$numPages;$i++){

($i!=$page)?$output.='<a href="'.$_SERVER
['PHP_SELF'].'?
page='.$i.'">'.$i.'</a>&nbsp;':$output.=$i.'&nbsp;';

}

// create next link

if($page<$numPages){

$output.='&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?
page='.($page+1).'">Next&gt;&gt;</a> ';

}

// return final output

return $output;

}

Although the function is basic, it's really flexible and portable, because it allows us to specify a data file to read from, and the number of records (in this case we're referencing file lines and not true database records) to be displayed at a time. Also, it's powerful enough to generate the classic <previous> - 1, 2, 3 <next> navigational links. It's an elemental format, but with minor modifications, it might be changed to present a more polished look, just by tweaking the original code.

One possible implementation for the function may be defined in the following example:

<?php

require_once('pager.php');

$page=$_GET['page'];

echo paginateRecords('data1.dat',$page);

?>

In the above example, we've specified a data file "data1.dat" from which to read, and a default value of five records to be displayed at a time. Because of its flexibility, another case may be coded as follows:

<?php

require_once('pager.php');

$page=$_GET['page'];

echo paginateRecords('data2.dat',$page,10);

?>

The previous case will display data from a "data2.dat" file and show ten records per page, reordering the paging links according to the new parameters. Definitely, we have to agree that our function is very versatile and extendable. Just play around with the code to make it fit your needs.

While the function is handy for small projects, it lacks some of the complexities found in more sophisticated applications. Therefore, as I previously stated, we're going to develop a PHP class that will paginate records from a data file. It will keep the functionality of the initial procedural method, but offer the wide advantages inherent to Object Oriented Programming. So, let's start defining the PHP class.



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