我正在構建一個產品目錄,我想根據用戶設備來確定。即,如果Windows設備顯示特定產品。我有我的產品上市絕對好,但是當執行分頁,我卡住了。僅在一頁上顯示的PHP分頁產品
我只想每頁列出6項,我有更多的結果。然而,更多結果顯示在第一頁上。當我選擇下一步時,身份證號碼會發生變化,以顯示它向我顯示下一個結果,但它繼續顯示相同的頁面,所以很顯然,我錯過了/與代碼混淆的地方。我會很感激任何建議:
$current_url = $_SERVER['REQUEST_URI'];
$current_url = substr($current_url, 1);
$results = mysqli_query($conn, "SELECT * FROM apps A INNER JOIN device D ON D.DeviceID = A.DeviceID WHERE D.DeviceName = '$os'");
$foundnum = mysqli_num_rows($results);
if ($foundnum==0){
echo "Sorry, there are currently no applications that are compatible with your device. Please try another option.";
} else {
echo "$foundnum applications are avaliable for '$os' devices:<p>";
$per_page = 6;
$start = $_GET['start'];
$max_pages = ceil($foundnum/$per_page);
if(!$start)
$start=0;
while($obj = $results->fetch_object()){
$applicationid=$obj->ApplicationID;
$start=0;
echo "<div class=\"col-4 col-sm-4 col-lg-4\">";
echo '<form method="post" action="cart_update.php">';
echo "<div id='product'><a href='appproduct.php?id=$applicationid'><img src='images/app_images/$applicationid.jpg' alt='Product picture'/></div>";
echo '<h2>'.$obj->ApplicationName.'</h2>';
echo '<p>'.$obj->ApplicationDescription.'</p>';
if($obj->App_cost=="0.00"){
echo '<p>Free</p>';
}else{
echo '<p>£'.$obj->App_cost.'</p>';
}
echo '<button class="add_to_cart">Add To Cart</button>';
echo '<input type="hidden" name="product_code" value="'.$obj->ApplicationID.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form><br /><br /></div>';
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$last = $max_pages - 1;
if($max_pages > 1){
//previous button
if (!($start<=0))
echo "<a href='index.php?os=$os=Search+source+code&start=$prev'>Prev |</a> ";
//pages
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++){
if($i == $start){
echo " <a href='index.php?os=$os=Search+source+code&start=$i'><b> $counter |</b></a> ";
}
else {
echo " <a href='index.php?os=$os=Search+source+code&start=$i'> $counter |</a> ";
}
$i = $i + $per_page;
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='index.php?os=$os=Search+source+code&start=$next'> Next</a> ";
}
echo "</center>"
?>
請修正大括號和縮進,因爲此時代碼很難讀取。您還應該嘗試通過數據庫進行分頁,而不是加載所有結果。如果您有100000條記錄,並且只想顯示其中的前6條,則需要獲取可能不會使用的99994條記錄。嘗試使用sql的「限制」。 – mareckmareck
在提出建議後,現在正在進行分頁。但是,選擇轉到第2頁時,分頁不顯示已選擇第2頁,它看起來像用戶在第1頁上,而「下一個」選項不會刪除,假設計數在某處出錯?我在另一頁上有這個分頁,它按我期望的那樣工作,但不在這個頁面上。 – Kiwi
確保您單擊「下一步」按鈕正確傳遞下一個值範圍的參數,並確保它們正確放入查詢中(顯然,每次單擊按鈕時必須重複該查詢)。 – mareckmareck