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

The final round: completing the "displayRecords()" method - PHP

In part three of our series about paginating records with PHP, we will learn how to make our application work with a relational database system such as MySQL. We will develop a tight, compact PHP class, capable of performing the most common paging tasks efficiently.

TABLE OF CONTENTS:
  1. Previous or Next? Paginating Records with PHP - Part 3
  2. Object-oriented paging: anatomy of the "Pager" class
  3. Controlling the look of database records: defining a template file
  4. Going deeper: a detailed coverage at the "displayRecords()" method
  5. The final round: completing the "displayRecords()" method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 16
May 10, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Completing the definition of this method is matter of adding to it the capabilities to handle any template file for formatting purposes, and generating the paging links. Actually, we're not so far from the finish line. Let's add the necessary code to work with template files and the paging links. Here are the additional lines to complete the method:

// previous method code goes here

// read template file contents

$templateContent=file($this->template);

// add template header to final output

$this->output=reset($templateContent);

// move pointer to placeholders line

$templateRow=next($templateContent);

// replace placeholders

while($row=mysql_fetch_row($result)){

$tempOutput=$templateRow;

for($i=0;$i<mysql_num_fields($result);$i++){

$tempOutput=str_replace('{data'.$i.'}',$row
[$i],$tempOutput);

}

// remove unpopulated placeholders

$this->output.=preg_replace("/
{data.}/",'',$tempOutput);

}

// add template footer to final output

$this->output.=end($templateContent);

// create paging links

$this->output.='<div id="'.$this->linksId.'">';

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

}

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

// return generated output

return $this->output;

The majority of the listed code is fully commented and quite easy to understand. Let's begin by explaining how the method will format records using a template file. First, it reads the template file contents, storing them in the $templateContent array, like this:

$templateContent=file($this->template);

Please, remember that the template file has three lines, defined as a header section, a placeholders section and a footer section. In order to format the records, the method first adds the header section to the final output, resetting the array pointer, and retrieving the first line of the template file, in the following way:

// add template header to final output

$this->output=reset($templateContent);

Now, it's time to move the pointers to the placeholders' line and replace them with the actual values:

$templateRow=next($templateContent);

// replace placeholders with actual data

while($row=mysql_fetch_row($result)){

$tempOutput=$templateRow;

for($i=0;$i<mysql_num_fields($result);$i++){

$tempOutput=str_replace('{data'.$i.'}',$row
[$i],$tempOutput);

}

// remove unpopulated placeholders

$this->output.=preg_replace("/
{data.}/",'',$tempOutput);

}

After replacing the placeholders with the corresponding values, any unpopulated variables are removed from the final output, substituting the placeholders with an empty string, this way enhancing the class' flexibility. Finally, once records have been properly formatted, the footer line is appended to the output:

// add template footer to final output

$this->output.=end($templateContent);

That efficiently completes the process of formatting the records. As you can see, the approach is flexible enough to change the overall visual appearance, by just changing simple template files.

The rest of the code is nearly identical to the example listed in the second part, where the paging links are created by appending a <previous> link whenever possible, then generating the numbered links, and finally inserting a regular <next> link, when applicable. The only noticeable change hangs on providing an ID to tie a CSS style to the links, and offering a more polished appearance:

// create paging links

$this->output.='<div id="'.$this->linksId.'">';

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

}

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

// return generated output

return $this->output;

Having completed the definition for our "displayRecords()" class method, the class is already set up to be implemented on any existing or future Web application. Hopefully we've demonstrated that coding a PHP paging class is a not only an instructive experience, but also fun!

Wrapping up

That's about it for the moment. In the final part of this series, we'll run through several examples to show how the class can be implemented in real-world applications, providing a useful way to introduce paginated result sets in websites. In the meantime, feel free to play with the class code, adding your own improvements to it. 



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