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
- <?php
- session_start();
- $find = strip_tags($find);
- $find = trim ($find);
- $find = strtolower($find);
- $_SESSION['find'] = $find;
- mysql_connect("mysqlv3", "username", "password") or die(mysql_error());
- mysql_select_db("sand2") or die(mysql_error());
- // We preform a bit of filtering
- $find = strip_tags($find);
- $find = trim ($find);
- $find = strtolower($find);
- $_GET['find'] = (isset($_POST['find'])) ? $_POST['find'] : $_GET['find'];
- $_GET['find'] = strtolower($_GET['find']);
- $result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'")
- or die(mysql_error());
- $_GET['result'] = $result;
- if(mysql_num_rows($result)>0){
- while($table=mysql_fetch_row($result)){
- $_GET[`$table[0]`] = $table;
- $_SESSION['table']= $table;
- $presult = mysql_query("SELECT COUNT(*) FROM `$table[0]`") or die(mysql_error());
- $rr = mysql_fetch_row($presult);
- $numrows = $rr[0];
- $rowsperpage = 20;
- $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;
- print "<p class=\"topic\">$table[0]</p>\n";
- $r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC LIMIT $offset, $rowsperpage");
- $_GET[`$r[0]`] = $r;
- print "<table class=\"navbar\">\n";
- while($row=mysql_fetch_array($r)){
- $_GET[`$row[0]`] = $row;
- $_SESSION['row']= $row;
- $effective_vote = $row['votes_up'] - $row['votes_down'];
- print "<tr>";
- print "<td>".'<a href="http://'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>";
- print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>";
- print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Vote.'</a>'.'</span>'."</td>";
- }
- print "</tr>\n";
- }
- print "</table>\n";
- $range = 3;
- /****** build the pagination links ******/
- // range of num links to show
- // if not on page 1, don't show back links
- if ($currentpage > 1) {
- // show << link to go back to page 1
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&find={$_SESSION['find']}'><<</a> ";
- // get previous page num
- $prevpage = $currentpage - 1;
- // show < link to go back to 1 page
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&find={$_SESSION['find']}'><</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='{$_SERVER['PHP_SELF']}?currentpage=$x&find={$_SESSION['find']}'>$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&find={$_SESSION['find']}'>></a> ";
- // echo forward link for lastpage
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&find={$_SESSION['find']}'>>></a> ";
- } // end if
- /****** end build pagination links ******/
- }
- else{
- print "None found";
- }
- //This counts the number or results - and if there wasn't any it gives them a little message explaining that
- $anymatches=mysql_num_rows($result);
- if ($anymatches == 0)
- {
- echo "Sorry, but we can not find an entry to match your query<br><br>";
- unset($_SESSION['find']);
- }
- }
- ?>