2013-04-27 82 views
-1

如何使期權價值一頁增加如果數據庫只每頁顯示至少5排優化PHP分頁

讓說,如果表中有20行,期權的價值將至少有4每頁選擇,原因只有5排頁面

<script type="text/javascript"> 
function submitPg() 
{ 
window.location = "report.php?selpg=" + document.AllIn.cbxpg.value; 
} 
</script> 
<?php 
if(!isset($_GET["selpg"])) 
{ $selpg = 1; } 
elseif ($_GET["selpg"] >= 1) 
{ $selpg = $_GET["selpg"]; } 
else 
{ $selpg = 1; } 
$selpgQQ = $selpg - 1; 
$selpgQ1 = $selpgQQ * 5; 
$selpgQ2 = $selpg * 5; 
$no = $selpgQ1 +1; 
?> 
<html> 
<body> 
<table id="tabel" border="1" cols="30" width="50%" bordercolor="blue" > 
    <tr bgcolor="gray"> 
     <th width="1%">No</th> 
     <th width="1%">Date</th> 
     <th width="1%">Time</th> 
     <th width="2%">Number</th> 
     <th width="10%">Message</th> 


    </tr> 
    <? 

    $no = 1; 
    $query =("SELECT * FROM mytable ORDER BY date ASC LIMIT $selpgQ1 , 5"); 
    $sql = mysql_query($query); 

while ($hasil = mysql_fetch_array ($sql)) { 
     $date = $hasil["date"]; 
     $time = $hasil["time"]; 
     $phone = $hasil["phone"]; 
     $message = $hasil["message"]; 
?> 
     <tr > 
     <td><center><?=$no?></center></td> 
     <td><?=$date?></td> 
     <td><?=$time?></td> 
     <td><?=$phone?></td> 
     <td><?=$message?></td> 
     </tr>  

    <? $no++; }?> 
<form name="AllIn" id="AllIn"> 
<select name="cbxpg" onchange="submitPg()"> 
<option value="0">Current Page : <?php echo $selpg; ?></option> 
<option value="1">Page 1</option> 
<option value="2">Page 2</option> 
</select> 
&nbsp;&nbsp;&nbsp; 
</form> 
</body></html> 
+0

請使用*在這裏提問時,你的變量可以理解*名字; '$ selpgQQ' ???對你的變量使用這種名稱會使你的代碼非常難以閱讀和理解 – thaJeztah 2013-04-27 16:10:22

回答

1

下面是我將如何做到這一點。這不會硬編碼顯示在單個頁面上的行數,因此稍後可以更輕鬆地進行自定義。如果您願意,您甚至可以使用另一個選擇菜單編輯頁數!按照評論。我認爲你應該檢查兩邊'selpg'的邊界:它可以是非積極的,也可以超過頁面的數量。

<?php 

// Set the number of rows to display on one page 
$rowsPerPage = 5; 

// Get the total number of rows in the table 
$rowsTotal = mysql_num_rows(mysql_query("SELECT * FROM mytable")); 

// To get the total number of pages, take the ceiling 
// of the total number of pages divided by the number 
// of pages to show on a single page 
$pagesTotal = ceil($rowsTotal/$rowsPerPage); 

// Get the current page number 
if (!isset($_GET['selpg'])) { 
    $pageNumber = 1; 
} else { 
    $pageNumber = $_GET['selpg']; 
    if ($pageNumber < 1) { 
     $pageNumber = 1; 
    } else if ($pageNumber > $pagesTotal) { 
     $pageNumber = $pagesTotal; 
    } 
} 

// Get the offset for the SQL query 
$offset = ($pageNumber - 1) * $rowsPerPage; 

// Set the iterator to start at the current offset 
$no = $offset; 

然後在你的MySQL查詢,你會寫:

$query = "SELECT * FROM mytable ORDER BY date ASC LIMIT $offset, $rowsPerPage"; 

最後,爲了使下拉動態填寫:

<select name="selpg" onchange="submitPg()"> 
    <?php 

    // To fill the dropdown dynamically, loop 
    // from 1 to the total number of pages 
    for ($i = 1; $i <= $pagesTotal; $i++) { 
     echo "<option value='$i'"; 

     // Immediately select the current page 
     if ($i == $pageNumber) echo " selected"; 

     echo ">Page " . $i . "</option>"; 
    } ?> 
</select> 

對於其餘的代碼,你可以使用':?>'爲了更好的可讀性。此外,你錯過了一個關閉</table>標籤。

<table id="tabel" border="1" cols="30" width="50%" bordercolor="blue" > 
    <tr bgcolor="gray"> 
     <th width="1%">No</th> 
     <th width="1%">Date</th> 
     <th width="1%">Time</th> 
     <th width="2%">Number</th> 
     <th width="10%">Message</th> 
    </tr> 
    <?php 

    // Preparing the query with limit and offset in place 
    $query = mysql_query("SELECT * FROM mytable ORDER BY date ASC LIMIT $offset, $rowsPerPage"); 

    while ($hasil = mysql_fetch_array($query)) :?> 
     <tr> 
      <td><center><?php echo $no++ ?></center></td> 
      <td><?php echo $hasil['date'] ?></td> 
      <td><?php echo $hasil['time'] ?></td> 
      <td><?php echo $hasil['phone'] ?></td> 
      <td><?php echo $hasil['message'] ?></td> 
     </tr> 
    <?php endwhile; ?> 
</table> 

爲了使JavaScript的拿起所選物品的價值(目前,使用「第5頁:」的「5」,而不是作爲價值不工作):

<script type="text/javascript"> 
function submitPg() { 
    var formById = document.forms["AllIn"]; 
    var selectMenu = formById.elements["selpg"]; 
    window.location = "report.php?selpg=" + selectMenu.value; 
} 
</script> 
+0

代碼完成這項工作,但是當我轉向第2頁時,表格行不會改變,它就像第1頁的表格行一樣 – 2013-04-27 15:50:40

+0

這很奇怪。我用我的本地數據庫中的其中一個表正常運行的代碼更新了我的答案。 如果這沒有幫助,試着用'echo'回顯一些變量,看看它們的值是否是你期望的值。 具體來說,您可以輸出您正在使用的整個SQL查詢,並查看限制和偏移是否正確。 – 2013-04-27 16:17:31

+0

mysql_fetch_array()期望參數1爲資源 – 2013-04-27 16:18:53

0

你需要讓行數在數據庫中,然後用5分取整數部分加1,如果其餘爲大於0所以,如果你有30行,你得到6頁,如果你有31行,你會得到7.

一旦你有您可以使用循環將選項添加到下拉菜單中。

+0

我該如何整合基於這個下拉菜單的循環? – 2013-04-27 15:20:38

+0

尋找其他答案。在mysql中使用select count(*)來獲取行數。代碼性能更好 – 2013-04-27 15:47:40