php - 搜索结果无法显示分页

标签 php mysql pagination

我刚刚学习分页。我无法让它适用于搜索结果。它正确显示第一页,并包含正确数量的链接,但单击任何链接(即使在第 1 页上)都会转到空白页。

有人可以告诉我我做错了什么吗:

以下代码适用于单击搜索按钮时的情况。

<div class="twelve columns">
        <div class="search">
            <div class="searchbox">
                <div id="form-container"> 
                        <div id="searchtext">
                        <form name="formSearch" method="GET" action="view_referral.php" >
                        <button class="gobtn">Search</button>
                        <input type = "text" id="s" name="searchField" size="25" placeholder="Search referrals...">
                        <input type ="submit" name="searchButton" value="Search" style="display:none;">
                        </form>
                        </div>
                </div>
            </div>
        </div>
    </div>

我已将分页代码放在一个单独的函数中: 这是查看我的分页的链接.. https://gist.github.com/markchristian27/4a3fbc3b34e9a78ccbcc

注意:只有当我尝试使用搜索功能时,链接到其他页面的分页才会失败。我已经在一个列出表格信息的页面上尝试过,并且链接工作正常。

<div class="referentprofile"> 
                            <p>List of Referrals</p>
                            <table class="u-full-width">
                                <tr>
                                <td><strong>Lead No.<strong></td>
                                <td><strong>Referral Name</strong></td>
                                <td><strong>Date Referred</strong></td>
                                <td><strong>Mobile Number</strong></td>
                                <td><strong>Email Address</strong></td>
                                <td><strong>Call Status</strong></td>
                                <td><strong>Application Status</strong></td>
                                </tr>
                            <?php

                        $_SESSION['SESS_PAGE'] = $_SERVER['PHP_SELF'];

                        // Change this during actual system run
                        if (empty($_GET['searchField'])){
                        include('../function/func_testPopulateLeads.php');
                                }
                                else {
                                    include('../function/func_testSearchByName.php');
                                    } 

                        ?>

                    </table>
                            </div>
                        <center>
                        <div style="text-align: center;">
                    <?php 
                    include('../function/func_pagination_new.php'); 
                    ?>
                    <?=$pagination?>
                </div>
                </center>   

分页和链接在 include('../function/func_testPopulateLeads.php'); 中工作正常但在 include('../function/func_testSearchByName.php'); 它不起作用也许在我看来问题出在链接中的信息:http://mainpage.com/refer/view_referral.php?searchField=mark告诉 searchField 要显示哪个页面,但它没有告诉它搜索信息是什么。如果没有它,您的搜索页面就不知道要显示什么。

我尝试将 searchField 的值连接到分页链接,但仍然无法正常工作。例如,我在搜索框中输入 mark。它的链接变成这样 http://mainpage.com/refer/view_referral.php?searchField=mark

然后,当我单击第2页时,它会更改为http://mainpage.com/refer/view_referral.php?page=2 这肯定会保留 mark 的值,因为我的搜索页面不知道当我转到下一页时要显示什么。如何解决这个问题?请花点时间阅读本文

最佳答案

代码中的主要问题是您使用链接进行分页,并且它没有将搜索词作为参数传递。

$pagination.= "<a class='buttons' href=\"$targetpage?page=$next\">next</a>";

这将导致使用参数“page=$next”转到目标页面。每次更改页面时,您都需要再次发送搜索词参数。

您可以将搜索字段存储在 view_referral.php 中并检索分页 php 文件中的值。

例如:

首先,将搜索值保存在view_referral.php中:

$_SESSION['searchField']=$_GET['searchField'];

然后,在分页 php 文件中,再次检索该值:

$pagination.= "<a class='buttons' href=\"$targetpage?page=".$next."&searchField=".$_SESSION['searchField']."\">next</a>";

这应该可以为您完成工作。

关于php - 搜索结果无法显示分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31667929/

相关文章:

mysql - AppMaker 记录保存失败 : 'id' field value is required, 但找到 'null' 。 (自动递增)

mysql - 带分页的搜索结果

php - 即使使用 DISTINCT,SQL SELECT 查询也会回显两次

php - 如何在 html 页面中显示数据库的值?

mysql - 如何获取插入行的 id?

php - MySQL 中使用链接动态排序

wordpress - 分页功能在 WordPress 中不起作用

jsf-2 - Primefaces 列切换器不适用于分页

php - 添加 Symfony 应用程序时出现 JSON_ERROR

php - opencart从excel导入用户