Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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;
}

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

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:

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

}

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

td {
  width: 33%;
  padding: 3px;
}

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

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!



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