如何为从 PHP 中的数据库获取的数据创建下一个、上一个链接


How to make next, previous links for data fetched from database in PHP?

我做了一个页面来从mysql数据库中获取数据。我只想显示每页 10 个结果,使用下一个按钮显示其他结果,使用上一个按钮显示上一个结果。

我使用以下代码来获取数据:

$item=$_POST('item');
$query=mysql_query("select name,address,contact from info_tb where keyword='$item'");
while($row=mysql_fetch_array('$query'))
{
echo "Name : row[0]":
echo "Address : row[1]":
echo "Contact : row[2]":
}

mySQL 查询中使用 LIMIT 关键字。

SELECT * FROM `your_table` LIMIT 0, 10 

似乎您想浏览记录,所以..

 SELECT * FROM `your_table` LIMIT 5, 10 

这些记录将显示 6、7、8、9、10、11、12、13、14、15

如果要导航,则需要如图所示控制此参数。

SELECT * FROM `your_table` LIMIT 5, 10
                                 ^  

似乎您最近发布了代码,请在代码上尝试这样的事情。

$item=$_POST['item'];
$page=$_POST['pno'];
$query=mysql_query("select name,address,contact from info_tb where keyword='$item' limit '$page',10");

对于您的第一页,您必须从数据库中检索您的前 10 个结果,即 1、2、3 ...9、10行:

SELECT * FROM `table` LIMIT 0, 10

并打印出您的结果。假设用户在第 4 页上,则必须从 31、32、33 中检索。39,40行。在像 PHP 这样的服务器脚本中计算偏移量和row_count:

$page_no = 4;
$row_count = 10; 
$offset = ($page_no - 1) * $row_count; // results 30

现在你的 SQL 将是:

SELECT * FROM `table` LIMIT 30, 10;

因此,您将拥有可以打印出的31,32,33...行。页码可以通过 GET 或 POST 参数检索。

当然,你可以打印出整个结果并使用jQuery对其进行分页:

<!-- Download jquery and link locally.  Directlinking to jQuery should only be used for testing purposes. -->
<script type='text/javascript' src='http://code.jquery.com/jquery-git2.js'></script>
<script type="text/javascript">
    var pageno = 0;
    var maxpageno = 0;
    function show(){
        if (pageno < 0) { pageno = 0}
        if (pageno > maxpageno) { pageno = maxpageno}
        $('#results tbody tr').hide();
        $('#results tbody tr.page'+pageno).show();
    }
</script>
<body onload="show();">
<?php
    $sql = "SELECT * FROM books";   
    //result is boolean for query other than SELECT, SHOW, DESCRIBE and EXPLAIN
    $result = $short_connect->query($sql);
    if (($result) && ($result->num_rows > 0))
    {
        $results = array();
        print "<table id='"results'">'n<thead>'n<tr><th>id</th><th>title</th><th>isbn</th><th>ean</th><th>year</th></tr>'n
        <tr><td colspan=5>
        <a href='"#null'" onclick='"pageno = 0; show();'">&lt;&lt;</a>
        <a href='"#null'" onclick='"pageno--; show();'">&lt;</a>
        <a href='"#null'" onclick='"pageno++; show();'">&gt;</a>
        <a href='"#null'" onclick='"pageno = maxpageno; show();'">&gt;&gt;</a>
        </th></tr>'n
        </thead>'n<tbody>'n";  //table starter and header
        $i = 0;                       //start count
        $ipp = 10;                        //items per page
        while ($row = $result->fetch_array())
        {
            $rop = $i % $ipp;           //result on page
            $pno = floor($i / $ipp);        //page number
            $i++;                         //increase count
            print "<tr class='"page$pno'"><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td></tr>'n";
        }
        print "</tbody>'n<tfoot><tr><td colspan=5>
        <a href='"#null'" onclick='"pageno = 0; show();'">&lt;&lt;</a>
        <a href='"#null'" onclick='"pageno--; show();'">&lt;</a>
        <a href='"#null'" onclick='"pageno++; show();'">&gt;</a>
        <a href='"#null'" onclick='"pageno = maxpageno; show();'">&gt;&gt;</a>
        </td></tr>'n</tfoot>'n</table>'n"; // footer
        $result->free();
    }
    $short_connect->close();
    print "<script type='"text/javascript'"> maxpageno = $pno;</script>";
?>
</body>