douweng7308 2018-06-01 16:44
浏览 69

将此分页脚本转换为搜索分页脚本的逻辑

I just spent sometime making the following tutorial work (I converted it to PDO instead of mysqli): PHP + MySQL Pagination tutorial This is how it looks:

<html>
<head>
    <title>Pagination</title>
    <!-- Bootstrap CDN -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
    <?php

        if (isset($_GET['pageno'])) {
            $pageno = $_GET['pageno'];
        } else {
            $pageno = 1;
        }
        $no_of_records_per_page = 10;
        $offset = ($pageno-1) * $no_of_records_per_page;

        $conn=mysqli_connect("localhost","my_user","my_password","my_db");
        // Check connection
        if (mysqli_connect_errno()){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            die();
        }

        $total_pages_sql = "SELECT COUNT(*) FROM table";
        $result = mysqli_query($conn,$total_pages_sql);
        $total_rows = mysqli_fetch_array($result)[0];
        $total_pages = ceil($total_rows / $no_of_records_per_page);

        $sql = "SELECT * FROM table LIMIT $offset, $no_of_records_per_page";
        $res_data = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($res_data)){
            //here goes the data
        }
        mysqli_close($conn);
    ?>
    <ul class="pagination">
        <li><a href="?pageno=1">First</a></li>
        <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
            <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
        </li>
        <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
            <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
        </li>
        <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
    </ul>
</body>
</html>

But I encountered a problem: This script is paginating only for SQL query that has been predefined. But what if I want to have a "search" where the pagination script starts to work only after a user hits "search" and not with the pre-defined query of "select *"? Is it possible with the above script or I need to change it?

  • 写回答

1条回答 默认 最新

  • drmg17928 2018-06-01 19:20
    关注

    Create HTML form, for example

    <form id="form" action="<?= $_SERVER['PHP_SELF'] ?>">
        <input type="text" name="search" value="<?= !empty($_GET['search']) ?
            htmlspecialchars($_GET['search']) : '' ?>">
        <input type="submit" value="Search">
        <input type="hidden" id="page" name="page" value="<?= !empty($_GET['page']) ?
            intval($_GET['page']) : '' ?>">
        <!-- add another fields here -->
    </form>
    ...
    <a href="" class="page">1</a>
    <a href="" class="page">2</a>
    

    and at the top of the script add:

    if (!empty($_GET['search']) { 
       // write SQL queries for pagination and other form processing logic here
    }
    

    To pass page parameter in URL, include jQuery to your script and add the following code to the head section of the page:

    $(document).ready(function(){
        $('.page').click(function(e) {
            e.preventDefault();
            $('#page').val($(this).html());
            $('#form').submit();
        });
    });
    

    This code blocks the default action for the link, set the page value to the form input with #page id and submits it.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?