2012-03-22 144 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