简体   繁体   English

如何使用php和mysql向我的基于Web的数据库添加分页

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

I've come across a problem - but any help would be appreciated. 我遇到了一个问题-但会有所帮助。

When I query the database using the results posted from a form, the pagination works initially ie for the first 10 records but when I click on the 2 hyperlink of the pagination for the second page of results it loses the $_POST variable and returns to the full data set. 当我使用从表单发布的结果查询数据库时,分页最初会起作用,即对于前10条记录,但是当我单击分页的2个超链接以获取第二页结果时,它将丢失$ _POST变量并返回到完整的数据集。

What is the best way of keeping these variables available for the second (and further) pages? 使这些变量可用于第二页(及更多页)的最佳方法是什么?

thanks for reply: still i have a problem, can any one help me please , the below is my complete php file thanks in advance 感谢您的答复:仍然我有一个问题,任何人都可以帮助我,以下是我完整的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>

The best practice says to not use POST method if your query doesn't alter the state of your system. 最佳做法是,如果查询不改变系统状态,则不要使用POST方法。 For example, in your case I wouldn't submit a POST request but I'd use GET. 例如,在您的情况下,我不会提交POST请求,但会使用GET。

That way your pagination links should preserve your query parameters. 这样,您的分页链接应保留您的查询参数。

Don't use POST in the first place. 首先不要使用POST。 POST is for sending data to be acted on. POST用于发送要执行的数据。 GET is for sending data that describes what you want to read. GET用于发送描述您要阅读的内容的数据。 If you are paginating, then you are reading things. 如果您正在分页,那么您正在阅读东西。

Then make sure your links contain all the data you need to describe what it is you want the second (or third or etc) page to contain. 然后确保您的链接包含描述第二页(或第三页等)要包含的内容所需的所有数据。

Its usefull to get a pre-written pagination class if you are inexperienced making one yourself. 如果您自己没有经验,可以使用它来获得预先编写的分页课程。 (if not ignore this awnser) (如果不忽略此遮篷)

You could use something like this 你可以用这样的东西

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM