PHP简单分页

时间:2017-01-01 20:43:11

标签: php pagination

以下代码是通过"选择选项表格"从DB获取一些值。 ,我最近添加了Pagination剪辑以限制结果,当我运行代码时,它按照定义获取5个记录器,但没有显示剩余页数。 我在这做错了什么?

<?php

$per_page = 5;
if (isset($_GET["page"])) {
    $page = $_GET["page"];
} else {
    $page = 1;
}

$start_from = ($page - 1) * $per_page;

if (!empty($_POST['form_val']) && isset($_POST['form_val'])) {
    $_POST['form_val'] = 0;

    $sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture  FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e 
    where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";

    if (!empty($_POST['Location']) && isset($_POST['Location'])) {
        $sql = $sql . " AND location =" . $_POST['Location'];
    }
    $strtdate = $_POST['Sday'];
    $enddate  = $_POST['Eday'];
    if (!empty($_POST['Sday']) && isset($_POST['Sday']) && !empty($_POST['Eday']) && isset($_POST['Eday'])) {
        $sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
    } elseif (!empty($_POST['Sday']) && isset($_POST['Sday'])) {
        $sql = $sql . " AND date>='" . $strtdate . "'";
    } elseif (!empty($_POST['Eday']) && isset($_POST['Eday'])) 
        $sql = $sql . " AND date<='" . $enddate . "'";
    }

    if (!empty($_POST['Site']) && isset($_POST['Site'])) {
        $sql = $sql . " AND u.site=" . $_POST['Site'];
    }

    $sql = $sql . " LIMIT $start_from, $per_page";

    if (mysqli_query($conn, $sql)) {
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) >= 1) {
            $rowcount = mysqli_num_rows($result);
            echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
            echo '<br><br>';
            echo "<table class='srchtable'>
                    <tr>
                    <th>Picture</th>
                    <th>Date</th>
                    <th>User Name</th>
                    <th>country</th>
                    <th>Location</th>
                    <th>Site</th>
                    <th>Comment</th>
                    </tr>";
            while ($row = mysqli_fetch_array($result)) {
                echo "<tr>";
                echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
                echo "<td>" . $row['date'] . "</td>";
                echo "<td>" . $row['user_name'] . "</td>";
                echo "<td>" . $row['country'] . "</td>";
                echo "<td>" . $row['location'] . "</td>";
                echo "<td>" . $row['site'] . "</td>";
                echo "<td>" . $row['comment'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
            $total_pages = ceil($rowcount / $per_page);
            echo "<center><a href='?page=1'>" . 'First Page' . "</a> ";
            for ($i = 1; $i <= $total_pages; $i++) {

                echo "<a href='?page=" . $i . "'>" . $i . "</a> ";

            }
            echo "<a href='?page=$total_pages'>" . 'Last Page' . "</a></center> ";
        } else {
            echo '<p>No Results Found !!!</p>';
        }
    }
}
?>

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,显示分页链接:

  • 您计算了总行数,但在LIMIT查询中加入了OFFSETSELECT条款,这不会给出正确的行数。您的SELECT查询不应包含此部分... LIMIT $start_from, $per_page
  • 由于您根据多个$_POST数据过滤了结果,因此您应该在分页链接中加入这些条件,否则当您访问其他页面时(通过分页链接),您赢了&# 39;获得所需的结果,因为当您从一个页面跳到另一个页面时,$_POST数据将不会被保留。 最好将method的{​​{1}}从<form>更改为POST ,因为这样对您来说更方便当你使用分页链接从一个页面跳到另一个页面时捕获和操纵事物。

基于以上几点,你的代码应该是这样的:

GET