Passing $_GET (or $_POST) Variables With Paginatio

2019-07-30 19:03发布

问题:

I know this is a common problem as I have searched for answers before deciding to post, but I can't seem to figure out a solution.

PROBLEM: I have a pagination script (PHP) to use for my search results. As is apparently common, the first page results show fine, then fail when moving onto page 2, 3 etc.

I get an 'unknown index' error for each of my variables used in the search when clicking through to page 2, 3 etc.

So I $_GET these variables from my form:

$_SESSION['var1']= $_GET['var1'];
$_SESSION['var2']= $_GET['var2'];
$_SESSION['var3']= $_GET['var3'];

Points to note: A Session has already been started in my header; I'm using $_GET because i prefer not having the 'resubmit' warning if a user goes 'back'; variables are all cleaned (just not shown in code as its long enough already); I have to use the $_GET variables with the WHILE loop as they calculate distance, age etc of each result.

My pagination script:

$limit = 4; 

$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

$stages = 3;
$page = mysql_escape_string(isset($_GET['page'])) ? (int)$_GET['page'] : 1;
if($page) {
  $start = ($page - 1) * $limit; 
} else {
  $start = 0;   
}   

// Get page data
$query1 = "SELECT * FROM $tableName ORDER BY joindate DESC LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;  
$next = $page + 1;                          
$lastpage = ceil($total_pages/$limit);      
$LastPagem1 = $lastpage - 1;                    

$paginate = '';
if($lastpage > 1) {
$paginate .= "<div class = 'hp1col'><div class='paginate'>";
$pagetotal = $total_pages.' Results';

// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
} else {
$paginate.= "<span class='disabled'>previous</span>";}

// Pages    
if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
{   
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{ $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                 
}
}

// Next
if ($page < $counter - 1){ 
$paginate.= "<a href='$targetpage?page=$next'>Next</a>";
} else {
$paginate.= "<span class='disabled'>Next</span>";
}
$paginate.= "</div></div>";     
}

while($row = mysql_fetch_array($result))
{
if (($part1 >= $_SESSION['var1']) AND  ($part2 <= $_SESSION['var2']) AND ($part3 <= $_SESSION['var3'])) {
echo 
"[Results]  
}
}
echo $paginate;

I tried starting a new session in this if statement but it didn't help:

if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
} else {
$paginate.= "<span class='disabled'>previous</span>";}

I hope someone can help. I apologise for the slab of code in the question, but I thought it best just to put everything in for ease in, hopefully, someone being able to help.

Thanks

回答1:

So you need to pass those query parameters through to the next page. If your page expects $_GET['var1'] to be present but you don't have ?var1=foo in the URL, it obviously won't work. The easiest way to handle this is http_build_query:

printf('<a href="%s?%s">Next</a>',
       $targetpage,
       http_build_query(array('page' => 2) + $_GET));

This preserves all current values in $_GET and adds a page=2 parameter to it. Modify as needed for your case.