2015-11-26 67 views
0

代碼:對於MySQL數據庫PHP分頁 - 默認顯示第一頁

$totalItemsRequired = 8; 
$query = "SELECT * FROM fruits ORDER BY origin ASC "; 
$result = mysqli_query($connection, $query); 

if ($result == false) 
    { 
    echo "<p>Selecting all fruits failed.</p>"; 
    } 
    else 
    { 
    $totalRecords = mysqli_num_rows($result); 
    $totalPages = ceil($totalRecords/$totalItemsRequired); 
    echo "<p>Page: "; 
    for ($i = 1; $i <= $totalPages; $i++) 
     { 
     echo "<a href='?page=" . $i . "'>" . $i . "</a> "; 
     } 

    echo "</p>"; 
    if (isset($_GET['page'])) 
     { 
     $currentPageNum = $_GET['page']; 
     $offset = ($currentPageNum - 1) * $totalItemsRequired; 
     $query = "SELECT * FROM fruits " . "LIMIT " . $offset . ", " . $totalItemsRequired; 
     $result = mysqli_query($connection, $query); 
     if ($result == false) 
      { 
      echo "<p>Selecting subset (page) of fruits failed.</p>"; 
      } 
      else 
      { 
      echo "<ul>"; 
      while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
       { 
       echo "<li>" . $row["id"] . "," . $row["name"] . ", " . $row["origin"] . ", " . $row["stock"] . "</li>"; 
       } 

      echo "</ul>"; 
      } 
     } 
     else 
     { 
     echo 

     // By default, load first page of records here 

     } 
    } 

?> 

嗨,我想在默認情況下,我已經把評論加載中記錄的第一頁。代碼沒有問題,初始輸出很好。我只是不知道如何顯示默認情況下。任何人都可以幫忙嗎?

編輯:任何人都可以幫忙嗎?

回答

1

相反的if語句,你就應該能夠改變

$currentPageNum = $_GET['page']; 
$offset = ($currentPageNum - 1) * $totalItemsRequired; 

$currentPageNum = isset($_GET['page']) ? $_GET['page'] : 0; 
$offset = $currentPageNum > 0 ? (($currentPageNum - 1) * $totalItemsRequired) : 0; 

全碼:

<?php 
$totalItemsRequired = 8; 
$query = "SELECT * FROM fruits ORDER BY origin ASC "; 
$result = mysqli_query($connection, $query); 

if ($result == false) 
    { 
    echo "<p>Selecting all fruits failed.</p>"; 
    } 
    else 
    { 
    $totalRecords = mysqli_num_rows($result); 
    $totalPages = ceil($totalRecords/$totalItemsRequired); 
    echo "<p>Page: "; 
    for ($i = 1; $i <= $totalPages; $i++) 
     { 
     echo "<a href='?page=" . $i . "'>" . $i . "</a> "; 
     } 

    echo "</p>"; 
     $currentPageNum = isset($_GET['page']) ? $_GET['page'] : 0; 
     $offset = $currentPageNum > 0 ? (($currentPageNum - 1) * $totalItemsRequired) : 0; 
     $query = "SELECT * FROM fruits " . "LIMIT " . $offset . ", " . $totalItemsRequired; 
     $result = mysqli_query($connection, $query); 
     if ($result == false) 
      { 
      echo "<p>Selecting subset (page) of fruits failed.</p>"; 
      } 
      else 
      { 
      echo "<ul>"; 
      while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
       { 
       echo "<li>" . $row["id"] . "," . $row["name"] . ", " . $row["origin"] . ", " . $row["stock"] . "</li>"; 
       } 

      echo "</ul>"; 
      } 
     } 
    } 

?> 
+0

你能否在整個代碼中實現這一點,因爲我不確定你的意思? – bob123

+0

@ bob123如果你沒有看到你的代碼的變化,那麼它可能不是你的代碼,因爲我已經在10秒內計算出它應用於哪個部分......並且我不是你的代碼的作者。 – KuKeC

+0

@KuKeC我不明白你的意思 – bob123

1

爲什麼不能在你的MySQL語句中使用LIMIT條款?

SELECT * FROM fruits ORDER BY origin ASC LIMIT 8 //First 8 rows of the table 
SELECT * FROM fruits ORDER BY origin ASC LIMIT 8,8 //Second 8 rows 
SELECT * FROM fruits ORDER BY origin ASC LIMIT 16,8 //Third 8 rows 

一般來說:

LIMIT offset,row_count將返回row_count行從offset+1行開始。