NYCPHP Meetup

NYPHP.org

[nycphp-talk] Using Pagination

drydell at optonline.net drydell at optonline.net
Thu Aug 23 17:46:41 EDT 2007


actually, the most effective technique is very simple... all you need is a query, the page number you want and the number of items on a page.First execute your query but don't fetch the resuts. Keep in mind is that executing a query takes a fraction of the time & resources it takes to fetch the result set and build an array - especially if it's a large result set. $qh = mysql_query($sql, [connection stuff]);All you want to fetch is the page of data you want to display, so you want to shift the result set's internal pointer to the right spot. The index you want will be (PageNumber -1) * numberOfItemsOnPage:mysql_data_seek($qh, $index); This is also very fast and will return false if the index is out of range.Now, you can do a fetch loop until you get a page's worth of rows or reach the end of the result set. No matter how big the result set is, you're always building a small array so it's pretty fast..If you'd like to display how many rows were returned, use mysql_num_rows($qh); This also uses the query handle and is also very fast.Since you can execute your query every time without a lot of overhead, it doesn't really matter how volatile the data is.While this example uses mysql, other engines have similar functions available too.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070823/9f8a60e8/attachment.html>


More information about the talk mailing list