在php中每页回显一定数量的结果

时间:2012-11-28 12:07:51

标签: php sql-server

我有一个查询和循环,根据ID显示产品。在这种情况下,子类别ID。代码如下:

<div id="categoryproducts">

  <?php
    $productsGet = mssql_query("SELECT * FROM Products WHERE SubCatID = ".$_GET['scid']."");
    while ($echoProds = mssql_fetch_array($productsGet)) {
                            ?>
    <div class="productbox">
       <div class="productboximg">
         <a href="product.php?pid=<?php echo $echoProds['ProductID']; ?>&cid=<?php echo $_GET['cid']; ?>"><img src="<?php echo $echoProds['ProdThumb']; ?>" height="58" width="70" alt="" /></a>
       </div>
    <div class="productboxdtl">
       <h3><a href="product.php?pid=<?php echo $echoProds['ProductID']; ?>&cid=<?php echo $_GET['cid']; ?>"><?php echo $echoProds['Title']; ?></a></h3>
       <p><?php echo $echoProds['Synopsis']; ?></p>
    </div>
      <div class="productboxprc">
        Price &nbsp; <strong>&pound;<?php echo $echoProds['Price']; ?></strong>
      </div>
    <div class="productboxmore">
       <a href="product.php?pid=<?php echo $echoProds['ProductID']; ?>&cid=<?php echo $_GET['cid']; ?>"></a>
    </div>
   </div>
   <?php
    }
   ?>
   <div id="shoplistpagesbot" class="shoplistpages">
    Results Pages: 1 <a href="productlist.php">2</a> [<a href="productlist.php?scid=<?php echo $_GET['scid']; ?>&cid=<?php echo $_GET['cid']; ?>" class="a1">Next &raquo;</a>]
   </div>

我不确定如何在每页显示一定数量的产品,如图所示有一种在页面之间切换的机制,我需要以某种方式编码,在一定数量的产品后,例如5,例如,其余的显示在下一页。

有人可以建议怎么做吗?或者指出我应该研究哪些功能的正确方向。

很抱歉,如果不是很清楚,我是PHP的新手。 DB im using是一个MS SQL而不是MySQL

2 个答案:

答案 0 :(得分:0)

$pagenumber = $_GET['pagenumber'];
$recordsperpage = 30;
$first = ($pagenumber*$recordperpage)-$recordperpage;
$last = $pagenumber*$recordsperpage;
$productsGet = mysql_query("SELECT * FROM Products WHERE SubCatID = '".$_GET['scid']."' LIMIT $first,$last");

将此设置在页面顶部,并在链接中传递GET参数,例如browse.php页面编号= 1;

您可以通过将记录集中的总行数除以$ recordsperpage变量来计算页数。

然后只需使用一个简单的for循环来输出导航链接,例如:

for($i = 1; $i <= $totalpages; $i++) {
echo "<a href='browse.php?pagenumber=$i'>$i</a>";
}

$ totalpages是将记录集中的总行数除以$ recordsperpage变量的结果。

希望这有帮助

这是MS SQL:

SELECT TOP 10 *
FROM (SELECT TOP 20 * FROM products ORDER BY ID) as T
ORDER BY ID DESC

基本上是在这里以相反的顺序从前20名中选出前10名记录。因此,您将获得记录集中的第二个10。

将10替换为每页记录数,将20替换为$ last变量。

希望这清除它

答案 1 :(得分:0)

取决于您使用的MSSQL版本。

我不是MSSQL的用户,但显然SQL Server 2000使用TOP命令,而SQL Server 2005使用BETWEEN命令。谷歌搜索应该为您提供几个教程,但我将假设您使用的是2005版本。

要返回页码$page_number上的项目,通用算法为:

// The most common way to specify which page to display is a GET variable. There
// are others ways and if you'd prefer them, just set $page_number to get the
// number from there instead. Don't forget to filter all data from the GET array
// as a user may try to insert harmful data, such as XSS attacks.
$page_number = $_GET['page'];
$scid = $_GET['scid'];
// Calculate the range of items to display.
$min = $page_number * $items_per_page;
$max = $min + $items_per_page;
$sql = "SELECT * FROM Products WHERE SubCatID = \"{$scid}\" BETWEEN {$min} AND {$max}";

要获取剩余的项目数,您需要一个单独的数据库查询来返回项目总数。

$sql = "SELECT COUNT(*) FROM Products WHERE SubCatID = \"{$scid}\"";
// Using the same $max as before as it is the number of items on the page plus
// total items on previous pages, but we'll redefine it here just in case.
$max = ($page_number * $items_per_page) + $items_per_page;
// Assume that $total_rows is the number returned from executing the count query.
$remaining_items = $total_rows - $max;

现在生成指向所有其他页面的链接。

$current_page = $_GET['page'];
$total_pages = $total_rows / $items_per_page;
if($current_page != 1) {
    $previous = $current_page - 1;
    echo "<a href=\"example.com/your/script.php?page={$previous}\" title=\"Previous Page\">Previous</a>";
}
for($i = 1; $i <= $total_pages; $i++) {
    echo "<a href=\"example.com/your/script.php?page={$i}\" title=\"Page {$i}\">{$i}</a>";
}
if($current_page != $total_pages) {
    $next = $current_page + 1;
    echo "<a href=\"example.com/your/script.php?page={$next}\" title=\"Next Page\">Next</a>";
}