2012-11-28 29 views
0

我有一個查詢和循環顯示產品取決於一個id。子類別id在這種情況下。代碼如下:每頁回顯一定數量的結果php

<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是一個MS SQL一個MySQL的不

+1

請參閱http://stackoverflow.com/search?q = [php] +分頁 – VolkerK

+0

只是一個提示:使用'mssql_ *'很好,但使用'PDO'可能會方便,它有一個[MS SQL驅動程序](http://be1.php.net/ manual/en/ref.pdo-sqlsrv.php),但是當你不得不切換到另一個數據庫(MySQL,Oracle,...)時,你不必重寫大量的代碼,而當你使用db特定的擴展,你會花費幾個小時重構所有與數據庫相關的代碼 –

回答

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>"; 
} 

其中$總頁數是你的$ 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變量。

希望這會清除它

+0

我覺得LIMIT是一個MySQL函數,在MS SQL中不可用。我對麼? – Bohdi

+0

問題是關於mssql,你是對的! – EJTH

+0

抱歉,是的,您需要使用MS SQL的TOP功能。 –

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>"; 
} 
相關問題