473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pagination - First page works great, next pages are blank

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
4 3539
prabirchoudhury
162 100+
hey, you dont need to assign $_session for the [ next ] [previous] page .. have a look on

Pagination Problem
May 28 '09 #2
Hi Prabirchoudhury,

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

Thanks,

John
May 28 '09 #3
prabirchoudhury
162 100+
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
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

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

Similar topics

11
by: ste | last post by:
Hi there, Further to my recent posts where I've received excellent help from Rik and Jerry, I've ended up with an image gallery on my website that displays images in a table, 3 images per row. ...
0
by: Sheau Wei | last post by:
I am using php version 4.3.3. The below was my pagination code. My problem is that when i run this code in my computer, i wall fail because of the hyperlink of the pagination not function when i...
3
by: assgar | last post by:
Hello I have a search page where you select the letter of the alphabet and the person's information who's name beginning with that letter is displayed. This function is placed on the web page...
1
by: assgar | last post by:
Hi I was using a schroll bar to display multiple rows of dynamically created from database records. The scrolling was not displaying the data properly so I have decided to use pagination. The...
1
by: shalini jain | last post by:
Hi, I want to know how can we do pagination using XSL. There are number of tutorials available on pagination using PHP but nothing with XSL. i am really stuck with my code. Below is the code that...
3
by: raaman rai | last post by:
Please help me with the following code. I have used this pagination script from the net and customized it but unfortunately it doesnt work as expected. The problem is, for the trial purpose i have 3...
16
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
1
markrawlingson
by: markrawlingson | last post by:
Hello, For starters: Yes, I am new to asp.net, however I hold a good 9-10 years of experience working with classic asp and am only just now upgrading my skills. I'm picking asp.net up pretty...
2
by: kkshansid | last post by:
this is my search page on which i am getting two parameters from previous page but the problem is that as soon as i click any other next pages my sql query fails as it doesnt get these two parameters...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.