Home arrow PHP arrow Page 4 - Previous or Next? Paginating Records with PHP, part 4

Paging MySQL records: putting the "Pager" class into action - 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.

  1. Previous or Next? Paginating Records with PHP, part 4
  2. A quick look at the class source code
  3. Creating the database table
  4. Paging MySQL records: putting the "Pager" class into action
  5. Extending the use of the "Pager class": more examples to come
By: Alejandro Gervasio
Rating: starstarstarstarstar / 16
May 17, 2005

print this article



Before showing any examples, a disclaimer is in order. Since we need to connect to the MySQL server, I will use a hypothetical MySQL class to perform the server connection. So, if you're seated on the procedural chair, don't worry. I'll take a look at that approximation too. Having said that, let's show the first example, first defining a simple default template file, as listed below:

  <tr class="header">
    <td>Song Id</td><td>Song Name</td><td>Song Author</td>

That's all that we need to format the result set. Definitely, you'll agree that this template file is very simple. Now, let's continue defining some CSS rules, in order to style the records and the paging links:

table {
  width: 50%;
  background: #ffc;
  font: normal 11px "Verdana", Arial, Helvetica, sans-serif;
  color: #000;
  text-align: center;
  border: 1px solid #000;

tr.header {
  background: #fc0;
  font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
  color: #000;

td {
  width: 33%;
  padding: 2px;

#paginglinks {
  position: absolute;
  top: 180px;
  left: 320px;
  font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
  color: #00f;

Finally, let's implement our "Pager" class, including the paging class file, a MySQL abstraction class, and the instantiation of the corresponding objects:

// include class files

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


Notice from the above example, that I've specified the display of five records per page, feeding the class with that incoming value.

For those Web developers connecting to MySQL using a procedural method, the above code can be rewritten as follows:

// include the pager class

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


Whatever method you use to connect to MySQL, the rendered output would look similar to this:

Not too bad, eh? Please notice how we've formatted database records and paging links, simply by using a three line template file. However, this is not the end. What if we change our mind and want to use another template file to build a different look and feel? Just go to the next page to find out more.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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