Previous or Next? Paginating Records with PHP, part 4 - Paging MySQL records: putting the "Pager" class into action (
Page 4 of 5 )
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:
<table>
<tr class="header">
<td>Song Id</td><td>Song Name</td><td>Song Author</td>
</tr>
<tr>
<td>{data0}</td><td>{data1}</td><td>{data2}</td>
</tr>
</table>
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:
<?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']);
?>
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:
<?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']);
?>
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.
| | 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! | | | | | |
|
 |