使用PHP搜索MySQL数据库并返回分页结果

时间:2012-03-22 05:41:35

标签: php

我遇到了一个问题,但任何帮助都会受到赞赏。 当我使用从表单发布的结果查询数据库时,分页最初工作,即前10个记录,但是当我点击第2页结果的分页的2超链接时,它会丢失$_POST变量并且返回完整的数据集。 保持这些变量可用于第二页(和更多页)的最佳方法是什么?

以下是我完整的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>

1 个答案:

答案 0 :(得分:0)

您必须传递过滤条件以及指向下一页的链接。

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

以及其他所有链接。