HomePHP Page 5 - Previous or Next? Paginating Records with PHP, part 4
Extending the use of the "Pager class": more examples to come - PHP
In the fourth and final article in our series about paginating records with PHP, we will look at the source code for the "pager" PHP class we developed in part three, and show several examples to demonstrate possible uses in production environments.
Say we want to use HTML lists to display the database records. Our new template file, called "record_template1.htm" would be defined in the following way:
<ul> <li>{data0} {data1} - {data2}</li> </ul>
But, wait a minute! Right now you must think that I'm kidding you. Not really. That's all we need to redefine the rendered output. Now, let's declare a few simple CSS rules:
li { font: normal 12px Arial, Helvetica, sans-serif; color: #009; }
Finally, let's implement the same PHP code, this time passing the new template file to our pager class:
<?php
// include class files require_once('pagerclass.php'); require_once('mysqlclass.php');
// instantiate a new MySQL object $db=&new MySQL('dbhost','username','password','songs');
// instantiate a new Pager object $pager=&new Pager($db->getConnectionId(),'SELECT * FROM songs','paginglinks',5,'record_template1.htm');
// display paginated result set echo $pager->displayRecords($_GET['page']);
?>
Or we can use the procedural approach, with the following lines:
<?php
// include the pager class require_once('pagerclass.php');
// connect to MySQL $conId=@mysql_connect('dbhost','username','password') or die ('Error connecting to the server: '.mysql_error());
// select database mysql_select_db('songs') or die ('Error selecting database');
// instantiate a new pager object $pager=&new Pager(&$conId,'SELECT * FROM songs','paginglinks',5,'record_template1.htm');
// display records and paging links echo $pager->displayRecords($_GET['page']);
?>
Our new paginated result set, using unordered lists, will be displayed as follows:
Can you see what a difference changing the template file made? The power and flexibility of the pager class is really remarkable. Want to see more? Fine, let's use the same default record template file defined in the first example, but this time change only the CSS rules. Here are the new CSS styles:
As usual, let's implement the PHP code to instantiate the pager class:
<?php
// include class files require_once('pagerclass.php'); require_once('mysqlclass.php');
// instantiate a new MySQL object $db=&new MySQL('dbhost','username','password','songs');
// instantiate a new Pager object $pager=&new Pager($db->getConnectionId(),'SELECT * FROM songs','paginglinks',5);
// display paginated result set echo $pager->displayRecords($_GET['page']);
?>
Alternatively, let's take the procedural road to make the MySQL connection:
<?php
// include the pager class require_once('pagerclass.php');
// connect to MySQL $conId=@mysql_connect('dbhost','username','password') or die ('Error connecting to the server: '.mysql_error());
// select database mysql_select_db('songs') or die ('Error selecting database');
// instantiate a new pager object $pager=&new Pager(&$conId,'SELECT * FROM songs','paginglinks',5);
// display records and paging links echo $pager->displayRecords($_GET['page']);
?>
According to the new CSS rules, the revamped visual output would be the following:
Certainly, the class shows how easy it is to change styles for records and display them in a paginated manner. It's just a matter of changing the template files, and we're in business.
Having shown all of these examples, I believe that I provided you with a wealth of resources for building paginated Web pages. Feel free to use this samples and build your own template files and CSS styles. If you've been feeling creative lately, let your inspiration fly and find new styles to utilize. The possibilities are endless.
Conclusion
We have finished our long examination of paging techniques. From the first article onward, I've demonstrated different methods for paginating web pages in a progressive learning curve. I wound up the process by developing a solid and robust PHP pager class, capable of rendering paged MySQL result sets with facility and flexibility.
For those developers working in the area of recordset paging, this class can certainly be very useful and highly portable. Personally, I've been using the class in several projects with excellent results, so with minor or no modifications, it might be the next solution for your paging needs. Good luck and happy paging!