milo Posted September 2, 2007 Share Posted September 2, 2007 I have a form that passes a single variable via the GET function to a mysql databse query with the results paginated. The first page of the results work fine, but the link to the second page yields the page 2 results of the entire table. I'm guessing that the variable hasn't passed to page2 query? Please excuse my ignorance. Here's my code: <?php //where $rory is the variable that is passed via the form with the GET action $page = $_GET['page'] $rory=$_GET['rory']; mysql_connect("localhost","<user>","<password>") or die(mysql_error()); mysql_select_db("<db>") or die(mysql_error()); $limit = 10; // Change to how many results you want per page $query_count = "SELECT * FROM bootlist WHERE venue LIKE '%$rory%' || location LIKE '%$rory%' || cd1 LIKE '%$rory%' || cd2 LIKE '%$rory%' || date LIKE '%$rory%' ORDER BY date"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); $query = ("SELECT * FROM bootlist WHERE venue LIKE '%$rory%' || location LIKE '%$rory%' || cd1 LIKE '%$rory%' || cd2 LIKE '%$rory%' || date LIKE '%$rory%' ORDER BY date LIMIT $limitvalue, $limit"); $result = mysql_query($query); $num=mysql_num_rows($result); if(mysql_num_rows($result) == 0){ echo("Nothing to Display!"); } while ($row = mysql_fetch_array ($result) ) { echo "Date: ".$row['date']; echo "<br>Venue: ".$row['venue']; echo "<br>Location: ".$row['location']; echo "<br>CD 1: " .$row['cd1']; echo "<br>CD 2: " .$row['cd2']; echo "<hr>"; } if($page != 1){ $pageprev = $page-1; echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV ".$limit."</a> "); }else{ echo("PREV " .$limit." "); } $numofpages = $totalrows / $limit; for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } if(($totalrows % $limit) != 0){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } if(($totalrows - ($limit * $page)) >=0){ $pagenext = $page+1; echo(" <a href=\"$PHP_SELF?page=$pagenext\">NEXT ".$limit."</a>"); }else{ echo(" NEXT " .$limit); } mysql_free_result($result); ?> Link to comment https://forums.phpfreaks.com/topic/67604-solved-passing-a-variable-to-paginated-query-results/ Share on other sites More sharing options...
php_tom Posted September 2, 2007 Share Posted September 2, 2007 Your code is long and a bit hard to read because you didn't use [ code ] tags, but maybe this is the problem: echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV ".$limit."[/url] "); I think this should be echo("<a href=\"$PHP_SELF?page=$pageprev&rory=".$_GET['rory']."\">PREV ".$limit."[/url] "); And similarily for the other parts where you echo the 'next 10' links, etc.... Link to comment https://forums.phpfreaks.com/topic/67604-solved-passing-a-variable-to-paginated-query-results/#findComment-339615 Share on other sites More sharing options...
milo Posted September 3, 2007 Author Share Posted September 3, 2007 It works like a charm. Thanks very much. I had tried to pass the second variable in the URL but I must have messed up the syntax. My apologies for not commenting out the code. Link to comment https://forums.phpfreaks.com/topic/67604-solved-passing-a-variable-to-paginated-query-results/#findComment-340278 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.