ArizonaJohn Posted May 22, 2009 Share Posted May 22, 2009 Hello, This is sort of a re-post. I am trying to get a table to paginate based on 20 rows per page. With the help of Crayon Violent's tutorial, I have almost gotten it to work. The code below does indeed split the first 20 rows into page one, and just as I want, there are hyperlinks for the following pages at the bottom of the page. The problem is that when I click on one of the hyperlinks, instead of bringing up the next 20 rows or the last rows or the next page (depending on the hyperlink), all that comes up is a blank page. Any ideas on why this is happening? Thanks, John <? //This is only displayed if they have submitted the form if ($searching =="yes") { //If they did not enter a search term we give them an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; unset($_SESSION['find']); } // Otherwise we connect to our Database mysql_connect("mysqlv3", "username", "password") or die(mysql_error()); mysql_select_db("sand2") or die(mysql_error()); $find = strip_tags($find); $find = trim ($find); $find = strtolower($find); $result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'") or die(mysql_error()); if(mysql_num_rows($result)>0){ while($table=mysql_fetch_row($result)){ $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"); print "<table class=\"navbar\">\n"; while($row=mysql_fetch_array($r)){ $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"; } /****** build the pagination links ******/ // range of num links to show $range = 10; // 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'><<</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 // 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'>$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 ******/ 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']); } } ?> Quote Link to comment Share on other sites More sharing options...
ArizonaJohn Posted May 22, 2009 Author Share Posted May 22, 2009 OK, I think I'm close to figuring it out. My table name is a session variable, so I need to somehow use session_start(); when I click over to the following pages. Where exactly could I put session_start(); so that my session variable is carried over into the next pages? Thanks, John Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 Right at the top. Although, I reckon it would be simpler to put the page in the query string of the url. Like ?page=2 or whatever. Quote Link to comment Share on other sites More sharing options...
ArizonaJohn Posted May 22, 2009 Author Share Posted May 22, 2009 When I click on the hyperlink to page two, the URL ends in this: tsearch14.php?currentpage=2 I have a file for tsearch14.php, and at its top I already had "session_start();" I don't have a file with code for the page tsearch14.php?currentpage=2, so I'm not sure where to put the "session_start();" And I think that the reason the results are blank on this page is because my session variable is not carrying over to this page. How can I put "session_start();" at the top of tsearch14.php?currentpage=2? Thanks Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 ...huh? Quote Link to comment Share on other sites More sharing options...
ArizonaJohn Posted May 22, 2009 Author Share Posted May 22, 2009 What I'm saying is that it seems to me that "tsearch14.php" and "tsearch14.php?currentpage=2" are two different pages, and that the session variable is only live for "tsearch14.php." If I'm right, then I need to figure out how to make the session variable live for "tsearch14.php?currentpage=2." Any ideas? Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 They're both the exact same script, just with a different query string. IE, $_GET['currentpage'] will be 2 in the second case and null on the first. Quote Link to comment Share on other sites More sharing options...
ArizonaJohn Posted May 22, 2009 Author Share Posted May 22, 2009 Hmm. Any idea why the results are blank, then, when I click to a page other than the first one? Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 Idk, I'm not entirely sure how that script works as I didn't write it. Try echoing out your queries to see if they're what you think they are. 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.