【问题标题】:Pagination script doesn't work with datatables分页脚本不适用于数据表
【发布时间】:2019-10-11 09:11:51
【问题描述】:

我已经处理了两天多的问题,但仍然找不到问题所在。我正在尝试为我的搜索结果构建一个分页系统。当我在新的 php 文件中运行代码时,代码工作得非常好,但是当我在表格中显示这些结果时,我不断收到该错误!最后一个 else 部分内的消息。另外,无论我在哪里放置页码的循环,它总是显示在表格之前。想想我忙于处理这个问题,我正专注于同一点。请帮忙!

edit 我刚刚删除了所有条件语句,并为我通过 POST 方法获得的所有变量获得了未定义的索引。这就是问题所在,但仍然不知道有什么解决方案。

<?php

if (isset($_POST['search_btn'])) {
include_once "db_connect.php";

$from = $_POST['from'];
$where = $_POST['where'];
$date = $_POST['date'];
$type = $_POST['type'];
$proc_id = $_POST['proc_id'];

if(empty($from) || empty($where) || empty($date) || empty($type)){
header("Location: index.php?search=empty");
exit();
}else{
//define how many results you want per page
$results_per_page = 10;

//number of results stored in database
"SELECT * FROM proc WHERE p_from = '".$from."' and p_where = '".$where."' and type= '".$type."' ";

$result = mysqli_query($conn, $sql);
$number_of_results = mysqli_num_rows($result);

//determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);

//determine which page number visitor is currently on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}

//determine the SQL LIMIT starting number for the result on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;

//retrive selected results from database and display them on page                                   $sql='SELECT * FROM proc LIMIT ' . $this_page_first_result . ',' .  $results_per_page;

$result = mysqli_query($conn, $sql);

while($rows = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

echo '<tr onclick="content(\''. $rows['proc_id'] .'\')">';
echo "<td>" .$rows['p_name'] . " " . $rows['p_surname']. " </td>";
echo "<td>" .$rows['p_from']. "</td>";  
echo "<td>" .$rows['p_where']. "</td>";
echo "<td>" .$rows['p_date']. "</td>";  
echo "<td>" .$rows['price']. "</td>";   
echo "<td>" .$rows['type']. "</td>";    
echo "</tr>";

 }
}
//display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '<a href="search.php?page=' . $page . '">' . $page . '</a> ';
                                        }

}else{
echo "Error!";
}

?>

【问题讨论】:

    标签: php html css pagination


    【解决方案1】:

    我只是假设初始显示有效,但是分页给您带来了问题?

    您正在使用 POST 值将数据从搜索表单传递到您的代码,但是,当您单击分页链接时,您将被转移到搜索页面,并丢失这些值。

    您可以将下一页 URL 更改为表单,并将每个必要的值作为隐藏输入字段传递。但是,这样做的缺点是当您在浏览器中点击返回按钮时,它会抱怨并要求您重新提交表单数据。

    另一种解决方案是将这些帖子参数存储在会话、cookie 中,无论什么。但在我看来,这也不是解决这个问题的好办法。

    我建议您使用 GET 参数,然后在下一页按钮中也传递这些参数。这还有一个额外的好处,就是能够为您的搜索添加书签。

    祝你好运!


    附带说明,您应该使用准备好的语句,而不是使用串联来构建查询。有关我在以下转换后的代码中使用的函数,请参阅文档:https://www.php.net/manual/en/class.mysqli-stmt.php

        if ($statement = mysqli_prepare($conn, "SELECT * FROM proc WHERE p_from = ? AND p_where = ? AND type = ?")) {
            mysqli_stmt_bind_param($statement, "s", $from);
            mysqli_stmt_bind_param($statement, "s", $where);
            mysqli_stmt_bind_param($statement, "s", $type);
    
            $result = mysqli_stmt_get_result($statement);
        }
    

    【讨论】:

    • 没错!回来删帖了,用GET方法终于成功了,也谢谢大家!您是否也知道为什么页码出现在顶部?
    • 页码显示在奇怪的地方可能是由于 css 或缺少 css。没有看到完整的页面很难说。我不擅长 css 和定位,所以你最好的办法是在完成的页面上查看你的 html 的实际外观并尝试使用它。
    猜你喜欢
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2019-02-17
    相关资源
    最近更新 更多