如何使用php和mysql将分页添加到我的基于web的数据库中


how to add pagination to my web based database using php and mysql

我遇到了一个问题,如果有任何帮助,我们将不胜感激。

当我使用表单中发布的结果查询数据库时,分页最初是有效的,即前10条记录,但当我点击第二页结果的分页的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>

最佳实践表明,如果查询不改变系统状态,就不要使用POST方法。例如,在您的情况下,我不会提交POST请求,但我会使用GET。

这样,分页链接应该保留查询参数。

首先不要使用POST。POST用于发送要操作的数据。GET用于发送描述要读取内容的数据。如果你在分页,那么你就是在读东西。

然后确保你的链接包含所有你需要的数据,以描述你希望第二页(或第三页等)包含的内容。

如果你自己没有经验,那么获得一个预先编写的分页类是很有用的。(如果不忽略此awnser)

你可以使用类似的东西

http://www.catchmyfame.com/2011/10/23/php-pagination-class-updated-version-2/