inherit
97216
0
Nov 26, 2024 13:53:14 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on Sept 17, 2010 17:38:22 GMT -8
How do you limit the results for a page? and then, any leftovers be displayed like display.php?page=2 ? This is gonna be used in my user.php page of epiboards.co.cc/uStation/ , an upload station. Thanks for any help!
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Sept 17, 2010 19:36:04 GMT -8
// if a page is set, and it's a number, and it's greater than 0 // use the page; otherwise, we're on page 1 $page = (array_key_exists('page', $_GET) && is_numeric($_GET['page']) && $_GET['page'] > 0 ? $_GET['page'] : 1); $items_per_page = 10; $results = mysql_query('SELECT `whatever` FROM `wherever` LIMIT ' . ($page - 1) * $items_per_page . ', '. $items_per_page) or die(mysql_error());
MySQL's LIMIT ability works as follows: LIMIT x, y Skip to the x'th result, and return y results after that.
So at page 1, we'd skip to the 0th result (which would be the first in an array; I may be wrong where LIMIT starts at 1 instead of 0, in which case you'd use ($page - 1) * $items_per_page + 1), and get 10 items after that. Then at page 2, we'd skip to the 10th result ($page - 1) * $items_per_page = 1 * 10 = 10 and get the 10 items after that. Then at page 3, we'd skip to the 20th result 2 * 10 = 20 and get the 10 items after that.
And there you have it. For every page, skip 10 and return the 10 after that.
|
|