Fluoresce Posted September 16, 2008 Share Posted September 16, 2008 I found an excellent script for pagination on PHPFreaks.com, written by some guy called Crayon Violent. I set it up on my site but have encountered a problem . . . It displays navigation links like this: 1 2 3 > >> Even if no results are returned from the database, the pagination links are still there: 1 2 3 > >> If 2 or 3 are clicked, they go to blank pages. I've played with $range (see below) but the problem remains. When I change $range to 0, for example, the pagination links display like the following, even if no results are returned: 1 > >> If ">" or ">>" are clicked, they lead to blank pages. Anyone know how I can fix this? If no results are returned, I don't want links to blank pages. Below's the full code. However, I think only the end bit is relevant. Thank you for any help in advance. <?php // database connection info $conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM tcoupon"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 5; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... 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; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $sql = "SELECT * FROM wherever LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); if(mysql_num_rows($result) < 1) { echo "<p>No results.</p>"; } else { while($row = mysql_fetch_assoc($result)) { // echo data echo "Results"; } // end while } // end else /****** build the pagination links ******/ // if not on page 1, show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // end if // range of num links to show $range = 3; // loop to show links to range of pages around current page for ($x = (($currentpage - $range) - 1); $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 " [$x] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$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='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ ?> Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 16, 2008 Author Share Posted September 16, 2008 Anyone? Quote Link to comment Share on other sites More sharing options...
peranha Posted September 16, 2008 Share Posted September 16, 2008 On the page do yo get "No results" with the linkable carrots (< << >> >) Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 16, 2008 Author Share Posted September 16, 2008 On the page do yo get "No results" with the linkable carrots (< << >> >) Hi, Peranha! Yes, it says "No results!" on the page, and the pagination links are there, too, even though there's no results! All of them (2, 3, ">" and ">>") link to blank pages. Any ideas? Quote Link to comment Share on other sites More sharing options...
peranha Posted September 16, 2008 Share Posted September 16, 2008 Then change these lines { echo "<p>No results.</p>"; } to { echo "<p>No results.</p>"; die(); } Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 16, 2008 Author Share Posted September 16, 2008 Then change these lines { echo "<p>No results.</p>"; } to { echo "<p>No results.</p>"; die(); } Peranha, it kind of worked! Thank you very, very much! I say it kind of worked because, you know the "Build the pagination links" section in my code? I had copied and pasted that above the bit that echos the results, so that the pagination links would appear both above and below the results. After applying your suggestion, if no results are returned for a query, just the bottom pagination links don't appear. The ones at the top do appear. Even though you've been very helpful already - which I very much appreciate - do you know why this might be? How can I get both sets of pagination links not to appear if there are no results? Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 16, 2008 Author Share Posted September 16, 2008 Then change these lines { echo "<p>No results.</p>"; } to { echo "<p>No results.</p>"; die(); } Actually, I've just noticed a problem . . . When I try what you said, Peranha, nothing at the bottom of my page appears, including my footer! Half my site's missing! Any other ideas, Sir? Quote Link to comment Share on other sites More sharing options...
peranha Posted September 16, 2008 Share Posted September 16, 2008 The logic of the page will have to be redone. A die statement stops the script from running after that point. Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 16, 2008 Author Share Posted September 16, 2008 The logic of the page will have to be redone. A die statement stops the script from running after that point. Apparently, my problem is that my script doesn't check if the proceeding pages contain any records. for ($x = (($currentpage - $range) - 1); $x < (($currentpage + $range) + 1); $x++) { It loops through all the ranges, but doesn't do anything additional to make sure that the data for page $x exists. That's why the links always appear whether or not there is data for the page. Do you how I might be able to do this? Quote Link to comment Share on other sites More sharing options...
peranha Posted September 16, 2008 Share Posted September 16, 2008 You can try this and see if it works the way you want it to. I rearrange the logic a little, and I didnt test it, but should work. <?php // database connection info $conn = mysql_connect('localhost','root', 'pass') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('database', $conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM table"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 5; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... 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; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $sql = "SELECT * FROM table LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); if(mysql_num_rows($result) > 1){ while($row = mysql_fetch_assoc($result)) { // echo results from search. echo "Results"; } // end while /****** build the pagination links ******/ // if not on page 1, show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // end if // range of num links to show $range = 3; // loop to show links to range of pages around current page for ($x = (($currentpage - $range) - 1); $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 " [$x] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$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='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ } // end else else { echo "<p>No results.</p>"; } ?> Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 16, 2008 Author Share Posted September 16, 2008 Thank you very much for your help, Peranha! Your code partly works. If there are no results, the pagination links don't show - just like we wanted! However, if there's, say, just a few results, the pagination links for all pages (1 2 3 > >>) appear again! Once again I've got links to blank pages! This problem's driving me CRAZY! Quote Link to comment Share on other sites More sharing options...
sabo86 Posted September 17, 2008 Share Posted September 17, 2008 I got the same problem.. when you click on the last page, it's blank!!! what's the solution? Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 17, 2008 Author Share Posted September 17, 2008 I got the same problem.. when you click on the last page, it's blank!!! what's the solution? Hey, Sabo86! I have no answer yet. I'm hoping that one of these PHP pros can save the day. If you get answer, make sure to send it my way, too. Quote Link to comment Share on other sites More sharing options...
peranha Posted September 17, 2008 Share Posted September 17, 2008 This is what I use for my pagination, I have png files with arrows on them, and it shows them, but it doesnt make the link active if there is only 1 page, or no pages at all. If you want, just put your < > in the place of the images and it will show them instead. It should work. if ($pageno == 1) { echo "<img src='pics/first.png' alt='' /> <img src='pics/previous.png' alt=''/> "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'><img src='pics/first.png' alt=''/></a> "; $prevpage = $pageno-1; echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'><img src='pics/previous.png' alt=''/></a> "; } // if echo " ( Page $pageno of $lastpage ) "; if ($pageno == $lastpage) { echo " <img src='pics/next.png' alt=''/> <img src='pics/last.png' alt=''/>"; } else { $nextpage = $pageno+1; echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'><img src='pics/next.png' alt=''/></a>"; echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'><img src='pics/last.png' alt=''/></a>"; } // if Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 17, 2008 Author Share Posted September 17, 2008 Peranha, thank you very much! I am very grateful. However, I need to make an apology, because I have just understood something very important . . . There's nothing wrong with my script! Nor is there anything wrong with Sabo86's script, I suspect. The number of pagination links that are displayed on any one page following a query depend on how many rows I have in my database and how many rows I want to be displayed on each page. For example, if there were 10 rows in my database and I wanted to show 5 per page, links to pages 1 and 2 would appear on my page, thus: 1 2 > >> And if there were 15 rows in my database and I wanted to display 5 per page, links 1, 2 and 3 would appear on my page, thus: 1 2 3 > >> No more than links 1, 2 and 3 would ever appear because I have set $range to 3. Now, if I had 15 rows in my database, even if my SELECT query returned no rows, I would still have links 1, 2 and 3 on my page because the number of links present on my page will always depend on how many rows I have in my database and how many rows I want to display on my page. Therefore, the only way to control the number of links that will actually appear on my page per each query is to make sure that the SELECT COUNT(*) query only counts the rows that will be selected during the main query and not all of my rows. In other words, the exact same rows must be taken into account by both of the following queries: $sql = "SELECT COUNT(*) FROM table WHERE . . ."; $sql = "SELECT * FROM table WHERE . . . LIMIT . . ."; As such, I realise that I have made a mistake: in fact, there really isn't a problem with my script! I therefore want to extend a sincere apology to you, Peranha, for wasting your time. Sorry, dude, and thank you very much for your help. Regards, Fluoresce, London Quote Link to comment Share on other sites More sharing options...
peranha Posted September 17, 2008 Share Posted September 17, 2008 Not a problem, glad it is working fine. Please mark the topic as solved. There is a button on the bottom left under the last post. Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 17, 2008 Author Share Posted September 17, 2008 You can try this and see if it works the way you want it to. I rearrange the logic a little, and I didnt test it, but should work. <?php // database connection info $conn = mysql_connect('localhost','root', 'pass') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('database', $conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM table"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 5; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... 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; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $sql = "SELECT * FROM table LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); if(mysql_num_rows($result) > 1){ while($row = mysql_fetch_assoc($result)) { // echo results from search. echo "Results"; } // end while /****** build the pagination links ******/ // if not on page 1, show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // end if // range of num links to show $range = 3; // loop to show links to range of pages around current page for ($x = (($currentpage - $range) - 1); $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 " [$x] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$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='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ } // end else else { echo "<p>No results.</p>"; } ?> One quick thing, Peranha, before I mark the thread as "Solved" . . . Can you tell me, please, what you did to stop my pagination links from showing when no results are returned? I can't figure it out. ??? Quote Link to comment Share on other sites More sharing options...
peranha Posted September 17, 2008 Share Posted September 17, 2008 I just moved the no results section to the bottom of the page, and the links in the loop that if results are found. 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.