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

Source code ahead: completing 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

Let's jump directly into the code to complete the method definition. Once the page pointer has been properly validated, it's time to fetch the exact number of records specified for being displayed on each page. Since we've already stored the total number of records as a class property $this->totalRecs, the process of extracting the corresponding records is a breeze. We just use the array_slice() PHP function to position the array internal pointer at the right place, and retrieve the correct number of records. This simple line extracts the records from the $this-> totalRecs array, as listed below:

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

Now, if the page pointer $page is equal to 1, and assuming that the default number of records is 5, the $records variable will store the first five elements from the $this->totalRecs array. Similarly, if $page is equal to 2, the second set of five records will be stored. As you can see, the expression is simple but extremely powerful for extracting chunks of records, closely resembling the LIMIT SQL statement extensively used with MySQL.

At this point, the method only needs to loop over the extracted records and progressively generate the output to be displayed in the browser. Using a couple of "foreach" loops, the task is nicely performed:

foreach($records as $row){

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

foreach($columns as $column){

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

}

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

}

By now, we've applied very basic formatting to the records. However, this might be changed with minor modifications. Before I hear someone complaining about this, in Part III of this article, we'll cover in-depth record formatting to suit real-world needs. So, you don't have any excuses to skip the next part of the series!

The rest of the code is nearly identical to the example shown in the first part. The methods take care of creating the paging links, first checking whether it's possible to insert a <previous> link. If it's possible, the corresponding link is generated. Then, the numbered links are created using a loop, and appending the $page variable to the querystring, providing the necessary navigational links.

Finally, the methods verifies whether it's feasible to generate a <next> link, to completely display the paging links.

Here are the lines used to create the paging links:

// create previous link

if($page>1){

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

}

// create numerated 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> ';

}

We've retrieved the records to be displayed and built the paging links with a single method. Definitely, the method shows its power and simplicity with some lines of code. Maybe we're not ready to paginate thousands of records, but this attempt is fairly valid!

Well, our job is almost done by now. But, am I forgetting anything? Sure, we need to put the class into action and see its actual functionality. Therefore, let's write a bit more of code to implement our "Pager" 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: