Previous or Next? Paginating Records with PHP - Part 3 - The final round: completing the "displayRecords()" method
(Page 5 of 5 )
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).'"><<Previous</a> ';
}
// create intermediate links
for($i=1;$i<=$numPages;$i++){
($i!=$page)?$this->output.='<a href="'.$_SERVER
['PHP_SELF'].'?page='.$i.'">'.$i.'</a> ':$this-
>output.=$i.' ';
}
// create next link
if($page<$numPages){
$this->output.=' <a href="'.$_SERVER
['PHP_SELF'].'?page='.($page+1).'">Next>></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).'"><<Previous</a> ';
}
// create intermediate links
for($i=1;$i<=$numPages;$i++){
($i!=$page)?$this->output.='<a href="'.$_SERVER
['PHP_SELF'].'?page='.$i.'">'.$i.'</a> ':$this-
>output.=$i.' ';
}
// create next link
if($page<$numPages){
$this->output.=' <a href="'.$_SERVER
['PHP_SELF'].'?page='.($page+1).'">Next>></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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |