Previous or Next? Paginating Records with PHP, part 4 - Extending the use of the "Pager class": more examples to come (
Page 5 of 5 )
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!
| | Discuss Previous or Next? Paginating Records with PHP, part 4 | | | | | | | The final part of the series shows several examples to use the paging class with a... | | | | | | I really wish these articles had come out two months ago as it would have saved me a... | | | | | | Hello Frank,
Thank you for the compliments. I'm glad to see that this series has... | | | | | | It appears that you have to execute your query twice. Once, to get the number of... | | | | | | The query is run twice, so you're correct. Of course, in the situation where... | | | | | | First, thanks for your great series of articles. I am not a programmer, but I've... | | | | | | Hello Allen,
Thanks for the compliments about my articles. With reference to your... | | | | | | Thanks for your suggestions. I figured it out. | | | | | | I'm highly satisfied to hear about it. Thanks for your useful... | | | | | | Hi,
Can I ask you another question? I have a case where my query contains a... | | | | | | Hi Allen,
There are many possible solutions for your case. You're having that... | | | | | | Thanks again for all your help with this. It's taken me a couple of days to reply... | | | | | | Hi Allen,
I'm glad to hear that you got the script to work. About your question... | | | | | | Hello,
I really like this tutorial it is sort of a smaller version of a templating... | | | | | | Hello Daniel,
First of all, thanks for the comment on the article. Secondly,... | | | | | | hi
i found your article about paginating very useful. i am not a programmer, and i... | | | | | | hi again
i was not able to paste the html file -- may be coz of html tags. but... | | | | | | Hello teeyan,
I'm glad to know that my articles on building up paging links have... | | | | | | hi
thank you for replying. your explanation helped me a lot. instead of changing... | | | | | | Hello again,
Now that the code was fixed up, paging links are working fine. I'm... | | | | | | hey, it was that easy. i should have used my brain.
so i tried to fix a few things... | | | | | | Hello teeyan,
Good to know that your programming issue is already fixed up. With... | | | | | | hi
thank you for all your help. that was the last and only file that had a... | | | | | | oh, the 2nd last line didn't appear. i meant that i put the styleswitcher.js as an... | | | | | | Hello teeyan,
Don't feel dissapointed about web programming because you're really... | | | | | | Hi, has anyone else had the following problem: everything seems to work fine,... | | | | | | Hi, I posted a question yesterday about the class not working for me. I have solved... | | | | | | I'm a new-be to all of this coding
so if you can, please outline for me
what... | | | | | | I like your class Previous or Next? Paginating Records, it appears to be just what... | | | | | | I've used the class succesfuly until the query returned the image (BLOB) from mysql... | | | | | | Thank you for the kind comment on this article. I guess your question was pointed... | | | | | | >>> Post your comment now! | | | | | |
|
 |