2015-05-30 28 views
2

我將分頁添加到表格中,而我之前創建的表格並沒有能夠讓它正常工作。表格分頁不會在選擇時進入不同的頁面

表限制的作品,但就是這樣。如果我選擇「首頁,最後或頁碼」,它只是重新加載頁面,但新頁面不顯示。

如果我將頁面限制設置爲像5這樣的低數字並選擇'Last Page',那麼當頁面加載它時顯示= 1就像它不知道還有其他頁面一樣。

<?php 
$con = mysqli_connect("localhost","root","","bfb"); 
$per_page=20; 

    if(isset($_POST["page"])) { 

    $page = $_POST["page"]; 
    } 
    else { 
     $page = 1; 
    } 

//Page will start from 0 and multiply per page 
    $start_from = ($page-1)*$per_page; 

//Selecting the data from the table but with limit 
    $query = "SELECT * FROM orders LIMIT $start_from, $per_page"; 
    $result = mysqli_query($con, $query); 
?> 

<table class="tableproduct"> 
    <tr> 
     <th class="thproduct">Order ID</th> 
     <th class="thproduct">Customer Name</th> 
     <th class="thproduct">Product</th> 
     <th class="thproduct">Total Price</th> 
     <th class="thproduct"></th> 
     <th class="thproduct"></th> 
    </tr> 
    <?php 
     if($result){  
     while($row = mysqli_fetch_assoc($result)) : 
    ?> 
<form method="POST" action="orderhistory.php"> 
    <tr> 
     <td class="tdproduct"><?php echo $row['order_id']; ?> </td> 
     <td class="tdproduct"><?php echo $row['customer_name']; ?> </td> 
     <td class="tdproduct"><?php echo $row['product_name']; ?> </td> 
     <td class="tdproduct"><?php echo $row['total_price']; ?> </td> 
     <td class="tdproduct"><a href='editorderhistory.php?id=<?php echo $row['id']; ?>'>EDIT</a></td> 
      <input type="hidden" name="product_id" value="<? echo $row['id']; ?>"/> 
     <td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td> 
    </tr> 
</form> 
<?php 
    endwhile; 
    } 
?> 
        </table> 
<?php 
//Count the total records 
if ($result != false) { 
$total_records = mysqli_num_rows($result); 
    if ($total_records == 0) { 
     echo 'No results founds.'; 
    } 
} 

//Using ceil function to divide the total records on per page 
$total_pages = ceil($total_records /$per_page); 
?> 
<span class="spandarkblue"> 
<?php 
//Going to first page 
echo "<center><a href='orderhistory.php?page=1'>".'First Page'."</a> "; 

for ($i=1; $i<=$total_pages; $i++) { 

echo "<a href='orderhistory.php?page=".$i."'>".$i."</a> "; 
}; 
// Going to last page 
echo "<a href='orderhistory.php?page=$total_pages'>".'Last Page'."</a></center>"; 
?> 

任何想法?

+0

您的某個變量可能因某種原因未正確設置。調試它們的值應該是一個好的開始,即使使用die() – lucasnadalutti

+0

所有頁面鏈接(如first,pageno,last)指向頁面1還是指向url中的不同頁碼? –

+0

@SameerK他們都指向頁碼1 – Paul

回答

2

在您的代碼中發現以下問題。

1)您正嘗試使用POST從URL獲取頁面值,因爲您需要使用GET方法從URl中獲取值。使用POST將返回空值,所以$page值始終設置爲1

因此,使用$_GET["page"]代替$_POST["page"]

2)你正在考慮其執行查詢的行數準備分頁。但是,由於您添加了limits,因此您的$total_records始終等於$ per_page值或更少,導致只有一個頁碼。

使用下面的代碼生成pazination

$query1 = "SELECT count(*) as totalRecords FROM orders"; 
$result1 = mysqli_query($con, $query1); 
if ($result1) { 
     $row1 = mysqli_fetch_assoc($result1); 
     $total_records = $row1['totalRecords']; 
    if ($total_records == 0) { 
     echo 'No results founds.'; 
     } 
} 

而不是下面的代碼

if ($result != false) { 
    $total_records = mysqli_num_rows($result); 
    if ($total_records == 0) { 
     echo 'No results founds.'; 
    } 
} 

希望它可以幫助你。

+0

當我做了第二部分的答案時,它現在轉到與最後一頁不同的頁面,但它仍未顯示其他頁面。現在只顯示「First」「Last」。它也迴應了「未找到結果」......我將它設置爲每頁5個結果,並且我有20多條記錄。 – Paul

+0

搭售時,我在查詢中犯了錯誤,我沒有考慮行數。我編輯了我的答案,請檢查。 –

+0

知道了!我很感激。你知道是否有任何方法可以使Ajax工作,所以我不需要重新加載頁面? – Paul

0

您需要獲取所有數據瞭解全部的你的記錄,那麼你只顯示whithin一個for()循環所需要的數據,所以從查詢中刪除LIMIT $start, $per_page,在這個節點,你將永遠有20個或更少結果

在你while()將數據保存到數組是這樣的:

$index = 0; 
while($row = mysqli_fetch_assoc($result)) { 
    $ordID[$index] = $row["order_id"]; 
    // The rest of your data... 
    $index++; // Increment the index 
} 

然後你瓚只顯示所需要的數據:

for($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) { 
    echo $orderID[$i]; 
    // And so on... 
} 

min()函數返回兩個變量之間的最小值,以這種方式在最後一頁中,如果您有17個產品而不是20個,則不會有錯誤。

相關問題