For the sake of reusability, be sure to encapsulate it as much as possible - I like to have a "Paging" class that generates the next, previous, and/or page numbers based on the info I give it (total number of results, starting index, results per page) and that makes paging is easy to implement throughout the site.
If only I could convince my employers to move to an open source platform... M$ SQL Server has no "limit" or "offset", only "top"...
Jon
From: Jacob Fugal <[EMAIL PROTECTED]> Reply-To: BYU Unix Users Group <[EMAIL PROTECTED]> To: [EMAIL PROTECTED], BYU Unix Users Group <[EMAIL PROTECTED]> Subject: Re: [uug] PHP Batching Date: Mon, 07 Jul 2003 12:26:04 -0600
Adrian Madrid wrote:
Adrian,Is there a library of some sort to batch queries? I mean, to have something like first, second, third, etc. 10 rows of 250. Like,
Records (x - x) out of x << View Previous 10 - View next 10 >>
Any ideas/pointers? I don;t even know if batching is the right name.
I know exactly what you're talking about and if there is a library, I would definitely like to know about it. What we currently do at my work to this end is a combination of queries and CGI parameters (using MySQL and Perl). One query counts the total number of possible rows, then using that number and info provided in the query (with suitable defaults, such as going to the first page if no page is specified) the links to other pages is built. The second query actually gets the info for just the relative rows using "... limit x,y". I don't know if this mysql specific or general SQL. Example:
<?php
if (!$page) $page = 0;
# $record_count = "select count(*) from myTable where condition=1" $page_size = 10; $max_page = ciel($record_count / $page_size) - 1; $start_record = $page * $page_size;
# while ($record = "select * from myTable where condition=1 limit $start_record, $page_size")
{
displayRecord($record);
}
if ($page > 0) { ?><a href="<?= $PHP_SELF ?>?page=<?= $page - 1 ?>">Previous Page</a><br /><?php }
if ($page < $max_page) { ?><a href="<?= $PHP_SELF ?>?page=<?= $page + 1 ?>">Next Page</a><br /><?php }
?>
Obviously pseudocode, don't expect it to run right out of the box. :) But it should give you an idea.
Jacob
____________________
BYU Unix Users Group http://uug.byu.edu/ ___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE* http://join.msn.com/?page=features/junkmail
____________________
BYU Unix Users Group http://uug.byu.edu/ ___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list
