优化PHP分页

时间:2013-04-27 14:36:14

标签: php pagination

如果数据库每页至少显示5行,如何使选项值增加一页

假设表格中有20行,由于每页只选择了5行,因此选项值将至少有4页

<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>

2 个答案:

答案 0 :(得分: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>

对于代码的其余部分,您可以使用':?&gt;'为了更好的可读性。此外,您错过了结束</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获取所选项目的值(目前,使用'Page: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>

答案 1 :(得分:0)

您需要获取数据库中的行数,然后除以5.取整数部分,如果其余部分大于0,则加1。如果有30行,则得6页,如果有的话31行,你得到7。

完成后,使用循环将选项添加到下拉列表中。