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

Defining the "displayRecords()" method - 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

The "displayRecords()" method is the workhorse of the class, and accepts one parameter, $page, which determines which page of the retrieved records we want to see. So, let's start by defining its signature and then explain how it works:

function displayRecords($page){

// calculate number of pages

$numPages=ceil(count($this->totalRecs)/$this->numRecs);

// validate page pointer

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

$page=1;

}

// retrieve corresponding records from flat file

$records=array_slice($this->totalRecs,($page-1)*$this-
>numRecs,$this->numRecs);

foreach($records as $row){

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

foreach($columns as $column){

$this->output.=$column.'&nbsp;';

}

$this->output.='<br />';

}

// create previous link

if($page>1){

$this->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)?$this->output.='<a href="'.$_SERVER
['PHP_SELF'].'?page='.$i.'">'.$i.'</a>&nbsp;':$this-
>output.=$i.'&nbsp;';

}

// create next link

if($page<$numPages){

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

}

// return final output

return $this->output;

}

As I said previously, the method accepts the $page parameter, which is the page pointer to determine which page we wish to view. However, before any validation process takes place, it calculates the number of pages needed to display the complete number of records. This value is obtained with the following lines:

// calculate number of pages

$numPages=ceil(count($this->totalRecs)/$this->numRecs);

Here we're dividing the total number of records read from the file by the number of records per page. Using ceil(), we round a decimal number up; in this way 3.25 becomes 4. Doing so, we get an integer value as the number of pages to be used.

Having obtained the number of pages, the method is now capable of validating the $page parameter. Maybe you're asking… why should I check out the validity of this parameter? The answer is that we don't want this value to be tampered with by external code. We're using the same technique shown in the first article. It's really funny to see how many websites fail to validate page pointers, and end up modifying paging links according to code injected through the querystring, even if they're using a URL rewriter. We don't want this to happen, so the method checks if the $page pointer is valid, like this:

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

$page=1;

}

Any attempt to tamper with the page pointer will result in a value of 1. Doing so, the method will display the first page. Simple and straightforward, right?

Are you getting tired of long explanations and want to see the class in action? So am I! But there are a few lines of code that still need to be explained. Just be a bit patient 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: