Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.
<?php
$start = (int)$_GET['start'];
$perpage = 25;//change this to whatever number you like.
$query = mysql_query("select ID from users where ID > 10 limit $start, $perpage");
while($data = mysql_fetch_array($query))
{
echo $data['ID']."<br >";
}
//nextpage(tablename,perpage,start,pageurl, where clause);
echo nextpage("users",$perpage,$start,$_SERVER['PHP_SELF'], "where ID > 10");
?>
UPDATE - 17/07/2004
Last edited by qads on Fri Jul 16, 2004 6:11 pm, edited 3 times in total.
There's a bit too much going on in that fn for my liking. What if you want to paginate something other than a query? You can't because the fn expects a query.
The fn also contains javascript. What if the user has js turned off?
PS: sorry if that sounds negative - not meant to be.
For the simple reason that in the situation of a large content your select is bringing an unwanted volume of information. You should add parameters to your select and in this way to bring only the range of the current page.
if it only counts the page range, then how will it know how many records there are in total?, it doest fetch anything from the db execpt the number of records.
btw, this is from ages ago, its just a copy and paste, i got a much better one now, with page numbers and all .
Should store the function output somewhere, as most people would want to show previous/next stuff at both the top and bottom of the page and you dont want to keep calling the database query.
right, just pasted the new one, remember, its just a paste, it was useing mysql class, so i had to chage it back to normal functions, but if u spot somethig, let me know.
fastfingertips wrote:I cannot agree with your snipet.
For the simple reason that in the situation of a large content your select is bringing an unwanted volume of information. You should add parameters to your select and in this way to bring only the range of the current page.
* This class might seem inefficient, exactly because it doesn't use mentioned
* database-specific SQL commands. However, for one very good reason, that
* isn't really true: it is almost always necessary to know the total number
* of pages in a query result - for example to be able to show a page
* navigator - and as this value is computed from the total number of rows in
* the query, the only way to get this number is by executing the full query.
* This completely eliminates the possible gain in efficiency when using
* <code>LIMIT</code>- or <code>SELECT TOP</code>-clauses, because in that
* case an additional (counting) query must be executed. Also, consider that
* most queries specify an ORDER BY clause, and remember that a DBMS can only
* compute this ordering by examining all rows in the result, even when just
* the first 10 are selected.
Not all dbs support LIMIT anyway.
Pagination all boils down to some pretty simple arithmetic which might be best encapsulated in its own class:
/*
$it - an iterator
$indexer - instance of Indexer, above
*/
for($it->reset($indexer->getOffset()); ($it->isValid() and $indexer->isValid()); $it->next())
{
// etc
}
As you may have noticed, reset() must be edited to accept an $offset arg and seek to that offset. Easy to do (and a default value of 0 allows reset() to behave as normal).
Last edited by McGruff on Mon Aug 08, 2005 6:57 pm, edited 1 time in total.