By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,670 Members | 1,526 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,670 IT Pros & Developers. It's quick & easy.

Pagination - First page works great, next pages are blank

P: 21
Hello,

The code below works great. The user enters a name into an HTML form, the code looks up a table with that name, and then that table is displayed.

I am trying to use pagination with it, and the pagination almost works. The first page of the pagination works fine, but when I click on one of the links for one of the next pages, the page is blank.

I have seen people mention this problem, and they have been told that a variable is not being passed to the next pages. So I have experimented passing variables using both the session method and the get method, and nothing seems to work. The code below shows "?currentpage=2&find=miami" at the end of the URL of the next pages for the topic "miami," but the page is blank.

Any ideas why this pagination is not working for the next pages?

Thanks,

John




Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. $find = strip_tags($find);
  4. $find = trim ($find);
  5. $find = strtolower($find);
  6. $_SESSION['find'] = $find;
  7.  
  8.  
  9.  
  10. mysql_connect("mysqlv3", "username", "password") or die(mysql_error());
  11. mysql_select_db("sand2") or die(mysql_error());
  12.  
  13. // We preform a bit of filtering
  14.  
  15.  
  16. $find = strip_tags($find);
  17. $find = trim ($find);
  18. $find = strtolower($find);
  19.  
  20. $_GET['find'] = (isset($_POST['find'])) ? $_POST['find'] : $_GET['find'];
  21. $_GET['find'] = strtolower($_GET['find']);
  22.  
  23. $result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'")
  24. or die(mysql_error());
  25.  
  26. $_GET['result'] = $result;
  27.  
  28. if(mysql_num_rows($result)>0){
  29. while($table=mysql_fetch_row($result)){
  30.  
  31. $_GET[`$table[0]`] = $table;
  32. $_SESSION['table']= $table;
  33.  
  34. $presult = mysql_query("SELECT COUNT(*) FROM `$table[0]`") or die(mysql_error());
  35.  
  36. $rr = mysql_fetch_row($presult);  
  37. $numrows = $rr[0]; 
  38. $rowsperpage = 20; 
  39. $totalpages = ceil($numrows / $rowsperpage);
  40.  
  41. // get the current page or set a default  
  42. if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  
  43.    // cast var as int  
  44.    $currentpage = (int) $_GET['currentpage'];  
  45. } else {  
  46.    // default page num  
  47.    $currentpage = 1;  
  48. } // end if  
  49.  
  50. // if current page is greater than total pages...  
  51. if ($currentpage > $totalpages) {  
  52.    // set current page to last page  
  53.    $currentpage = $totalpages;  
  54. } // end if  
  55. // if current page is less than first page...  
  56. if ($currentpage < 1) {  
  57.    // set current page to first page  
  58.    $currentpage = 1;  
  59. } // end if  
  60.  
  61. // the offset of the list, based on current page   
  62. $offset = ($currentpage - 1) * $rowsperpage; 
  63.  
  64.  
  65. print "<p class=\"topic\">$table[0]</p>\n";
  66. $r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC LIMIT $offset, $rowsperpage");
  67.  
  68. $_GET[`$r[0]`] = $r;
  69.  
  70. print "<table class=\"navbar\">\n";
  71. while($row=mysql_fetch_array($r)){
  72.  
  73. $_GET[`$row[0]`] = $row;
  74. $_SESSION['row']= $row;
  75.  
  76. $effective_vote = $row['votes_up'] - $row['votes_down']; 
  77.  
  78. print "<tr>";
  79.  
  80. print "<td>".'<a href="http://'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>";
  81. print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>";
  82. print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Vote.'</a>'.'</span>'."</td>";
  83. }
  84. print "</tr>\n";
  85. }
  86. print "</table>\n";
  87.  
  88.  
  89. $range = 3;  
  90.  
  91. /******  build the pagination links ******/  
  92. // range of num links to show    
  93.  
  94. // if not on page 1, don't show back links  
  95. if ($currentpage > 1) {  
  96.    // show << link to go back to page 1  
  97.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&find={$_SESSION['find']}'><<</a> ";  
  98.    // get previous page num  
  99.    $prevpage = $currentpage - 1;  
  100.    // show < link to go back to 1 page  
  101.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&find={$_SESSION['find']}'><</a> ";  
  102. } // end if   
  103.  
  104. // loop to show links to range of pages around current page  
  105. for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {  
  106.    // if it's a valid page number...  
  107.    if (($x > 0) && ($x <= $totalpages)) {  
  108.       // if we're on current page...  
  109.       if ($x == $currentpage) {  
  110.          // 'highlight' it but don't make a link  
  111.          echo " [<b>$x</b>] ";  
  112.       // if not current page...  
  113.       } else {  
  114.          // make it a link  
  115.      echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&find={$_SESSION['find']}'>$x</a> ";  
  116.       } // end else  
  117.    } // end if   
  118. } // end for  
  119.  
  120. // if not on last page, show forward and last page links      
  121. if ($currentpage != $totalpages) {   
  122.    // get next page  
  123.    $nextpage = $currentpage + 1;  
  124.     // echo forward link for next page   
  125.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&find={$_SESSION['find']}'>></a> ";  
  126.    // echo forward link for lastpage  
  127.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&find={$_SESSION['find']}'>>></a> ";  
  128. } // end if  
  129. /****** end build pagination links ******/  
  130.  
  131.  
  132. }
  133.  
  134.  
  135.  
  136. else{
  137. print "None found";
  138. }
  139.  
  140.  
  141.  
  142.  
  143. //This counts the number or results - and if there wasn't any it gives them a little message explaining that
  144. $anymatches=mysql_num_rows($result);
  145. if ($anymatches == 0)
  146. {
  147. echo "Sorry, but we can not find an entry to match your query<br><br>";
  148. unset($_SESSION['find']);    
  149.  
  150. }
  151.  
  152.  
  153. }
  154. ?>
  155.  
  156.  
May 27 '09 #1
Share this Question
Share on Google+
4 Replies


prabirchoudhury
100+
P: 162
hey, you dont need to assign $_session for the [ next ] [previous] page .. have a look on

Pagination Problem
May 28 '09 #2

P: 21
Hi Prabirchoudhury,

Could you be more specific? What variable does not need a $_session?

Thanks,

John
May 28 '09 #3

prabirchoudhury
100+
P: 162
ok..
you are doing simple pagination so for that you need
1. dont need to assign $_SESSION['find'] = $find; to store and carry your variable $find.

2. you are passing $find already through query string (with next and previous page url) , so from the next page you could get this $find frm $_GET[find] and use it and pass again through the query string.

3. and make sure you are getting all the variables you need for the next page.

echo all the variables and make sure you are nor missing passed to the next page.

may be your session is unset($_SESSION['find']); in the bottom condition
May 28 '09 #4

P: 21
Hi,

I tried using &find={$_GET['find']}, and the variable is indeed being passed through. I can tell by the URL of the next pages.

However, the results are still blank.

Maybe "find" is the wrong variable for me to be passing through? After all, this is the result that I am breaking up into pagination:

Expand|Select|Wrap|Line Numbers
  1. $result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'")
  2. or die(mysql_error());
  3.  
  4. if(mysql_num_rows($result)>0){
  5. while($table=mysql_fetch_row($result)){
  6.  
  7. print "<p class=\"topic\">$table[0]</p>\n";
  8. $r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC LIMIT $offset, $rowsperpage");
  9.  
  10.  
  11. print "<table class=\"navbar\">\n";
  12. while($row=mysql_fetch_array($r)){
  13.  
May 28 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.