2012-03-22 141 views
0

我遇到了一個問題,但任何幫助,將不勝感激。 當我使用從表單中發佈的結果查詢數據庫時,分頁工作最初適用於前10條記錄,但是當我點擊第2頁結果的分頁的2個超鏈接時,它將丟失$_POST變量並返回到完整的數據集。 保持這些變量可用於第二(和更多)頁面的最佳方式是什麼?使用PHP搜索MySQL數據庫並返回分頁結果

以下是我完整的php文件。

<html> 
<head> 
<link rel="stylesheet" type="text/css" 
href="design.css"> 
</head> 


<body> 

<?php 
include("header.php"); 
?> 
<center> 
<div id="content" class="frm"> 

<a href='admin.php' style='float:left'>Back!</a> 
<h2>Search Result</h2> 
<br><br> 
<?php 

include("../config.inc"); 
    $find=$_GET['find'];   
      // get page no and set it to page variable, if no page is selected so asign first page bydefualt 
      if (isset($_GET["page"])){ 
        $page = $_GET["page"]; 
       } 
       else { 
        $page=1; 
       } 
       // count all record in this table then divide it on 10 in order to find the last page----- every page has 10 record display 
        $sql = "SELECT COUNT(*) FROM tt where TTT='$find' "; 
        $rs_result = mysql_query($sql); 
        $row = mysql_fetch_row($rs_result); 
        $total_records = $row[0]; 
        $total_pages = ceil($total_records/2); 
        // this line check that page no must be in integer format 
        $page = (int)$page; 
        if ($page > $total_pages) { 
        $page = $total_pages; 
        } // if 
        if ($page < 1) { 
        $page= 1; 
        } // if 

        $start_from = ($page-1) * 2; 

$q=mysql_query("select * from tt where TTT='$find' order by ID limit $start_from,2"); 
$c=mysql_query("select count(*) from tt where TTT='$find'"); 
echo "<center>".mysql_result($c,0)."Filtered</center>"; 
echo "<center>"; 
echo "<table border='2' bgcolor=#CCCCCC> 
<tr> 
<th>TTT</th> 
<th>Enroll Date</th> 
<th>Gradution Date</th> 
<th>ID</th> 
</tr>"; 

while($row=mysql_fetch_array($q)) 
{ 
echo "<tr>"; 
echo "<td>".$row['TTT']."</td>"; 
echo "<td>".$row['Enroll_Date']."</td>"; 
echo "<td>".$row['Graduation_Date']."</td>"; 
echo "<td>".$row['ID']."</td>"; 
} 
echo "</table>"; 
echo "<center>"; 

       // paginatio start here 
       if ($page== 1) { 
       echo " << < "; 
       } else { 
      echo " <a href='{$_SERVER['PHP_SELF']}?page=1'><<</a> "; 
      $prevpage = $page-1; 
       echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'><</a> "; 
      } // if 
      echo " (Page [$page] of [$total_pages]) "; 

      if ($page == $total_pages) { 
      echo " > >> "; 
      } else { 
       $nextpage = $page+1; 
       echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>></a> "; 
       $lastpage=$total_pages; 
       echo " <a href='{$_SERVER['PHP_SELF']}?page=$lastpage'>>></a> "; 
       } // if 
?> 

</div> 
</center> 

<?php 
include("footer.php"); 
?> 
</body> 
</html> 
+0

將$ find添加到鏈接中,就像使用$ page一樣。這是大多數網站在搜索時所做的。 – 2012-03-22 05:54:02

回答

0

您必須將過濾條件和指向下一頁的鏈接一起傳遞。

echo " <a href='{$_SERVER['PHP_SELF']}?page=1&find=$find'><<</a> "; 

等與其他鏈接。

+0

Thanks Rev ,,,,, – kwahedi 2012-03-22 06:56:40