Using Paginator with mySQL
Paginator works very well with Mysql. You can easily use the base record and
the offset with LIMIT. Paginator comes with an extension, Paginator_html,
that at the moment provides a couple of pre-made link sets.
To divide mySQL result sets into pages you may do the following. I assume
you have made a connection to your database.
Include the Paginator class.
If you are using Paginator_html include that also.
Make a query to get the number of rows in your database. You might use something like this.
$num_rows = mysql_result(mysql_query("SELECT COUNT(*) FROM your table"),0);
Make a new Paginator object
$a =& new Paginator($_GET['page'],$num_rows);
If you are using Paginator_html make a new new Paginator_html object instead of a Paginator object.
$a =& new Paginator_html($_GET['page'],$num_rows);
Set the number of records to be displayed
If using numbered links this will set the number before and behind the current page.
Get the starting point.
$limit1 = $a->getRange1();
Get the number of items displayed on page.
$limit2 = $a->getRange2();
Make your query using LIMIT
$result=mysql_query("SELECT * FROM your table LIMIT $limit1, $limit2");
Output your results in your preferred manner
If you are using Paginator alone you will need to create your links here using the methods from
Paginator. See example3.php, example4.php.
If you are using Paginator_html you add the methods to add your links here
Will make a set of links like this: 5 of 8 of 25 First | Prev | Next | Last |
Will make a set of links like this: Previous 1 2 3 4 5 6 7 Next