jondo Posted July 18, 2006 Share Posted July 18, 2006 Hi guys,I am currently using this script for pagination[code]$limitpages = 5;$page_limit = $page + $limitpages;if ($page_limit > $total_pages ){$page_limit = $total_pages ;}$page_start = $page;$page_stop = $page_start + $limitpages;if ($page_stop > $total_pages){$page_stop = $page_stop -$total_pages ;$page_start = $page_start -$page_stop;}// Build Previous Linkif($page > 1){$prev = ($page - 1);$host = $_SERVER['PHP_SELF']; echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&page=$prev\"><</a> ";}for($i = $page_start; ($i <= $total_pages & $i <=$page_limit); $i++){if(($page) == $i){echo "$i ";} else { echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&page=$i\">$i</a> ";}}// Build Next Linkif($page < $total_pages){$next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&page=$next\">></a>";}echo "</div>";[/code]which is from a php tutorial here and modified with help from siguy to limit the number of pages.It works great, apart from 1 little thing which is that you can move forward fine by clicking on a page number, and the code will display a few links for pages behind and ahead of the current page. However if you then click the first link, after the page loads, the only way to go back is to keep clicking the back (<) button. I have tried to modify it myself but I kept getting infinite loops and browser crashes!If you want to see what I mean in action, click here:http://host.hostingseries44.net/~charles7/exhibition.php?category=8(this page will actually have around 1000 items in it soon, not the 40 odd just now, so it is necessary to limit the pages)thank you very much for your time. Quote Link to comment https://forums.phpfreaks.com/topic/14965-pagination-with-many-pages/ Share on other sites More sharing options...
foreverhex Posted July 18, 2006 Share Posted July 18, 2006 I've used this code also, so I might be able to help. Are you wanting it to show all the page numbers? Because this script limits how many pages that $i shows. Quote Link to comment https://forums.phpfreaks.com/topic/14965-pagination-with-many-pages/#findComment-60118 Share on other sites More sharing options...
akitchin Posted July 18, 2006 Share Posted July 18, 2006 alright, that math seems terribly convoluted given how simple this appears to be. assuming $page is the current page you're on, and $totalpages is the total number of pages returned for the query:[code]<?php// set the limit of pages to show after the current$upper_limit = 5;// set the limit of pages to show before the current$lower_limit = 5;// calculate where we're starting the loop, and where we're stopping$start = (($page - $lower_limit) <= 1) ? 1 : ($page - $lower_limit);$stop = (($page + $upper_limit) >= $totalpages) ? $totalpages : ($page + $upper_limit);// finally, show the links// show a back if neededif ($start > 1){ // show back button (i'm sure you can slot this in yourself)}// show the pages themselvesfor ($i = $start; $i <= $stop; ++$i){ // show the number link, if ($i == $page), then show the number but not the link}// show the next link if necessaryif ($stop < $totalpages){ // show the next button}?>[/code]should work, but to be fair i haven't tested it. give it a whirl and let me know if it comes up with errors. the code within the if()s and for() can just be slotted in from your current. Quote Link to comment https://forums.phpfreaks.com/topic/14965-pagination-with-many-pages/#findComment-60127 Share on other sites More sharing options...
jondo Posted July 19, 2006 Author Share Posted July 19, 2006 thanks mate but i'm afraid it didn't do anything, didn't actually display previous, numbers or next link(I did slot in the code you told me to)i have edited the original code i posted to this[code]$limitpages = 3;if(($page - $limitpages)<=1){ $pagestart==1;}else{ $pagestart==$page-$limitpages;}$page_stop = $page + $limitpages;if ($page_stop > $total_pages){ $page_stop==$total_pages;} // Build Previous Linkif($page > 1){$prev = ($page - 1);$host = $_SERVER['PHP_SELF']; echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&page=$prev\"><</a> ";}for($i = $page_start; $i <= $page_stop; $i++){if(($page) == $i){echo "$i ";} else { echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&page=$i\">$i</a> ";}}// Build Next Linkif($page < $total_pages){$next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&page=$next\">></a>";}echo "</div>";[/code]and this works as it should, except that it includes minus number pages (eg when on page 1, page links are "-2 -1 0 1 2 3 4" where it should start at 1, and when on the last real page (page 9), the page links are "6 7 8 9 10 11 12", where it should just be 6-9as before you can see what I mean in action here:http://host.hostingseries44.net/~charles7/exhibition.php?category=8any help much appreciatedcheers Quote Link to comment https://forums.phpfreaks.com/topic/14965-pagination-with-many-pages/#findComment-60372 Share on other sites More sharing options...
jondo Posted July 19, 2006 Author Share Posted July 19, 2006 ok I have changed the code at the top which sets the $page_start and $page_stop again, I don't have the minus numbers problem or the too many page links problem, however now, if the page I am on gets past 6 out of 9 (which is $totalpages - $limitpages), then the page number links won't display (ie on page 7, 8 and 9 only previous and next links show)this is the updated code[code]$limitpages = 3;if($page-$limitpages<1){ $page_start==1;}else{ $page_start = $page-$limitpages;}if ($page+$limitpages>$total_pages){ $page_stop==$total_pages;}else{ $page_stop = $page + $limitpages;}// Build Previous Linkif($page > 1){$prev = ($page - 1);$host = $_SERVER['PHP_SELF']; echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&page=$prev\"><</a> ";}for($i = $page_start; $i <= $page_stop; $i++){if(($page) == $i){echo "$i ";} else { echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&page=$i\">$i</a> ";}}// Build Next Linkif($page < $total_pages){$next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&page=$next\">></a>";}echo "</div>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14965-pagination-with-many-pages/#findComment-60387 Share on other sites More sharing options...
designationlocutus Posted July 19, 2006 Share Posted July 19, 2006 If you're getting from a database, you could make use of LIMIT in your SQL. It allows you to set offset and return parameters (e.g. LIMIT 10, 10 would start 10 records in and get the 10 records)You could have a two constant somewhere in your PHP file that sets how much you want on a page.For example $offset could be how many pages in and $returned could be the pages returned.Then, use the constants on the LIMIT. You could also use these constants as a way of calculating how many pages you have by dividing your total records (your total pages) by the constant. A result of 10.3, for example, would indicate 9 pages of 10 records and 1 page of 3 records.If you want to change your pagination at any time, you simply change the constants and it is changed throughout your entire code! Quote Link to comment https://forums.phpfreaks.com/topic/14965-pagination-with-many-pages/#findComment-60398 Share on other sites More sharing options...
jondo Posted July 19, 2006 Author Share Posted July 19, 2006 haha after trying to sort the logic of this for so long I missed the most obvious mistake!I found the problem in this line:if ($page+$limitpages>$total_pages){ $page_stop==$total_pages;}i used the comparison operator ==, instead of assignment statement =silly me...!all working nowthank you all very much for your help, and offers of helpcheers Quote Link to comment https://forums.phpfreaks.com/topic/14965-pagination-with-many-pages/#findComment-60412 Share on other sites More sharing options...
SammyP Posted July 19, 2006 Share Posted July 19, 2006 Reading down I was wondering if that had been picked up on. Was hoping it hadn't!!I've had so many hours of debugging time wasted on that problem and vice versa. Quote Link to comment https://forums.phpfreaks.com/topic/14965-pagination-with-many-pages/#findComment-60415 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.