DBookatay Posted November 28, 2008 Share Posted November 28, 2008 I have a script that displays how many pages (of search results) are available, like the one on this forum, however I do not know how to "trim" it, so it will look like my topic subject: Page: 1 [2] 3 4 5 6 ... 120 Currently it returns ALL 120 pages. Here is how I wrote the script, and ideas or changes would be greatly apprechiated... $offset = ($_GET['page']-1) * 20; $query = "SELECT count(*) as thecount from Sold $where"; $result = mysql_query($query); $row = mysql_fetch_array($result); $total = $row[thecount]; $pages = ceil($total/20); $query = "SELECT * FROM Sold $where ORDER BY $order limit $offset,20"; $result = mysql_query($query); $numrows = mysql_num_rows($result); for($x = 0; $row = mysql_fetch_array($result); $x++) { [bUNCH OF PHP STUFF GOES HERE] } Then on the bottom of the page, where the links are: Page: <? for($x = 1; $x<= $pages ; $x++) {if($_GET['page'] != $x) { echo '<a href="list.php?category='.$_GET['category'].'&page='.$x.'">'.$x.'</a>';} else {echo $x;}}?> Quote Link to comment Share on other sites More sharing options...
swizzer Posted November 28, 2008 Share Posted November 28, 2008 There are many different approaches to pagination as you will find out, but you are on the right path with two queries, one for the count, one for the information. Your format: Page: 1 [2] 3 4 5 6 ... 120 I'm assuming 120 is the last page ? You can have three branches of logic assuming you have more than 6 pages. $pagelinks = 6 //how many pages you wanna have //if close to 1 if ($page < $pagelinks){ [echo format that works for beginning] } elseif(($page + $pagelinks) > $lastpage){ [echo format that works in at the end] } else { //somewhere in the middle [echo format that works anywhere in the middle range] } Quote Link to comment Share on other sites More sharing options...
DBookatay Posted November 28, 2008 Author Share Posted November 28, 2008 how do I determine what $last is? "120" was just a number I picked as an example. Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted November 28, 2008 Share Posted November 28, 2008 Hi DBookatay, It might be worth your while following the PHP Freak basic tutorial on pagination: http://www.phpfreaks.com/tutorial/basic-pagination I'm not sure if it will cover what your looking for but its useful as it breaks down the code snippet by snippet! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.