Jax2 Posted April 13, 2010 Share Posted April 13, 2010 Evening all. Just wondering if someone can spot a problem in the following code. The pagination itself generally works, I.e., if I set the rowsperpage=5, it shows me 5 results before showing me the next/previous links, which also work fine and show me the correct results ... the issue is, if it is only one page, SOMETIMES it will show: page 1/1 [1] like it is supposed to, but most of the time, it will say: page 1/0 >> >>> And I don't really see how it is determining when to show next page links ... I can only guess it is some kind of math problem here, but like I said, sometimes it works, sometimes it doesn't ... For example, if I search for "a", it will find 5 records that contain "a" in the company name and show page 1/1 If I search for c, it will return the same 5 results (as they all have a c in them as well), but in this case, it will say page 1/0 and then show the next links, which when you click on them, shows you a blank page. Help?! Here's the code: if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; $sql ="SELECT COUNT(*) FROM XXXXX WHERE company_name like '%".$query."'"; $result=mysql_query($sql, $db); $r = mysql_fetch_row($result); $numrows = $r[0]; $totalpages = ceil($numrows / $rowsperpage); if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } MY NORMAL PAGE INFO HERE -- // BEGIN PAGINATION MAKE PAGE LINKS: echo "Page ".$currentpage." of ".$totalpages."<br>"; if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='XXXXX.php?orderby=search¤tpage=1&query=".$query."'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='XXXXX.php?orderby=search¤tpage=$prevpage&query=".$query."'><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='XXXXXX.php?orderby=search¤tpage=$x&query=".$query."'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " <a href=XXXXX.php?orderby=search¤tpage=$nextpage&query=".$query."'>></a> "; // echo forward link for lastpage echo " <a href='XXXXX.php?orderby=search¤tpage=$totalpages&query=".$query."'>>></a> "; } // end if Again, when there are 6, 7, 8 or even 30+ records, it shows the next pages just fine, correct amounts, everything, I'm only having this problem if there are 5 or less results, and, again, sometimes it shows it right (I.e., page 1/1 ( with no links)) ... I can't even begin to understand why it would do that. Thanks in advance, as always. Link to comment https://forums.phpfreaks.com/topic/198335-slight-problem-with-pagination/ Share on other sites More sharing options...
Jax2 Posted April 13, 2010 Author Share Posted April 13, 2010 Oh, yes, and it's also doing the same thing if I have 0 results... the pagination shows up as: Page 1/0 >> >>> doesn't even show the [1] , just the next, and last links ... Please see next reply, won't let me edit the original ... Link to comment https://forums.phpfreaks.com/topic/198335-slight-problem-with-pagination/#findComment-1040653 Share on other sites More sharing options...
Jax2 Posted April 13, 2010 Author Share Posted April 13, 2010 Sorry, not trying to bump this, but I found out what was causing part of the problem ... I was doing my initial search for company_name like %query when it should have been: like %query%, so that explains why it was showing the wrong page numbers ... The issue is still there however if there are NO results returned. It is showing Page 1/0 >> >>> when it should say Page 1/1 [1] Because while there's technically not even a single page of results, there are no extra pages so it needs to not show the links. That's all I need the help with ... Link to comment https://forums.phpfreaks.com/topic/198335-slight-problem-with-pagination/#findComment-1040657 Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 1. SQL query should have two wildcards. 2. If $currentpage is 1, then you will show two links that goes back to page 1. Is it this line that's printing out the value 0? echo "Page ".$currentpage." of ".$totalpages."<br>"; So $totalpages is 0? Link to comment https://forums.phpfreaks.com/topic/198335-slight-problem-with-pagination/#findComment-1040672 Share on other sites More sharing options...
Jax2 Posted April 13, 2010 Author Share Posted April 13, 2010 Yes, $totalpages does equal 0, but it shouldn't ... it should equal 1 ... the code is: $totalpages=ceil($numrows / $rowsperpage); If I read correctly, CEIL is supposed to return the next highest integer value by rounding up value ... Ahh ... there's more to that ... rounding up the value where needed, so, if it's 0, it doesn't need to round up to 1, because it's not 0.1, or 0.2, it's 0... So I need to add something there, such as, if $totalpages=0, $totalpages=1 ... Make sense? Just tried that, worked perfectly. Sheesh. Something simple causing such a headache. Grrr. Link to comment https://forums.phpfreaks.com/topic/198335-slight-problem-with-pagination/#findComment-1040680 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.