【问题标题】:Search Filters with pagination?带分页的搜索过滤器?
【发布时间】:2017-09-22 16:11:00
【问题描述】:

我有一个用于跟踪客户信息和设备的数据库。我有一个有效的搜索页面,将所有数据合并为可读格式。它设置为每页仅显示一个结果。

我正在尝试将过滤添加到搜索中。这样,如果您只搜索姓氏,就不会浪费时间搜索设备数据库。它只会搜索客户表中的姓氏字段。我有这个工作问题是当按下下一页时它得到零结果。

我知道为什么会这样。这是因为我使用 isset 和复选框来进行过滤。

$SLastName = $_GET['LastName'];
$SFirstName = $_GET['FirstName'];
$SAddress = $_GET['Address'];
$SAP = $_GET['AP'];
$SEquipment = $_GET['Equipment'];
$SEverything = $_GET['Everything'];

if (isset($SLastName)){
        $construct .="(customers.LastName LIKE '%$search_each%')";  
        if (isset($SFirstName)){
        $construct .=" OR (customers.FirstName LIKE '%$search_each%')"; 
        }
        if (isset($SAddress)){
        $construct .=" OR (customers.Address LIKE '%$search_each%')";
        }
        if (isset($SAP)){
        $construct .=" OR (aps.AP LIKE '%$search_each%')";
        }
        if (isset($SEquipment)){
        $construct .=" OR (equipment.ESN LIKE '%$search_each%')
                OR (equipment.WMAC LIKE '%$search_each%')
                OR (equipment.SN LIKE '%$search_each%')
                OR (equipment.Manufacturer LIKE '%$search_each%')";
        }
        if (isset($SEverything)){
        $construct .=" OR (customers.Account LIKE '%$search_each%')
                OR (customers.AlternatePhone LIKE '%$search_each%')
                OR (customers.Phone LIKE '%$search_each%')
                OR (routableip.routable_IP LIKE '%$search_each%')
                OR (unitip.UNITIP_new LIKE '%$search_each%')";
        }
    }
    else if(isset($SFirstName)){
        $construct .="(customers.FirstName LIKE '%$search_each%')";
        if (isset($SAddress)){
        $construct .=" OR (customers.Address LIKE '%$search_each%')";
        }
        if (isset($SAP)){
        $construct .=" OR (aps.AP LIKE '%$search_each%')";
        }
        if (isset($SEquipment)){
        $construct .=" OR (equipment.ESN LIKE '%$search_each%')
                OR (equipment.WMAC LIKE '%$search_each%')
                OR (equipment.SN LIKE '%$search_each%')
                OR (equipment.Manufacturer LIKE '%$search_each%')";
        }
    }
    else if(isset($SAddress)){
        $construct .="(customers.Address LIKE '%$search_each%')";
        if (isset($SAP)){
        $construct .=" OR (aps.AP LIKE '%$search_each%')";
        }
        if (isset($SEquipment)){
        $construct .=" OR (equipment.ESN LIKE '%$search_each%')
                OR (equipment.WMAC LIKE '%$search_each%')
                OR (equipment.SN LIKE '%$search_each%')
                OR (equipment.Manufacturer LIKE '%$search_each%')";
        }
    }
    else if(isset($SAP)){
        $construct .="(aps.AP LIKE '%$search_each%')";
        if (isset($SEquipment)){
        $construct .=" OR (equipment.ESN LIKE '%$search_each%')
                OR (equipment.WMAC LIKE '%$search_each%')
                OR (equipment.SN LIKE '%$search_each%')
                OR (equipment.Manufacturer LIKE '%$search_each%')";
        }
    }
    else if(isset($SEquipment)){
        $construct .="(equipment.ESN LIKE '%$search_each%')
                OR (equipment.WMAC LIKE '%$search_each%')
                OR (equipment.SN LIKE '%$search_each%')
                OR (equipment.Manufacturer LIKE '%$search_each%')";
}   

搜索输入页面使用复选框。在提交页面上选中这些框以查看设置了哪些字段。设置字段确定 sql 将是什么。

就像我说的那样,这对于找到第一个结果很有效。

这里是我的代码中与分页有关的部分。

$start = isset($_GET['start']) ? $_GET['start']: '';
    $max_pages = ceil($foundnum / $per_page);

    if(!$start)
        $start=0; 

//Pagination Starts
        $prev = $start - $per_page;
        $next = $start + $per_page;
        $adjacents = 3;
        $last = $max_pages - 1;

        if($max_pages > 1){   
        //previous button
            if (!($start<=0)) {
                $PrevButton .= "<a href='search.php?
search=$search&submit=Search&start=$prev' class='button Prev1' 
style='vertical-
align:middle'><span>Prev</span></a> ";    
            }
            else{
                $PrevButton .= "<a class='incative' style='vertical-
align:middle'><span>Prev</span></a> ";
            }
            $current =$next;



        //next button
        if (!($start >=$foundnum-$per_page)){
            $NextButton = "<a href='search.php?
search=$search&submit=Search&start=$next' class='button Next2' 
style='vertical-align:middle'><span>Next</span></a>";
        }
        else {
            $NextButton = "<a class='incative' style='vertical-
align:middle'><span>Next</span></a> ";
        }
        }

例如,如果我搜索姓氏 Smith。它将显示结果 1 of 10。然后,当我按下下一个按钮时,它会出现我内置的错误,因为它没有找到任何带有搜索关键字的结果。

我认为正在发生的事情是,当按下下一个按钮时,它会更改 GET url 以显示第 2 页,但是当发生这种情况时,它会丢失搜索 sql 的值,因为它不再看到 $SLastName isset。

我不知道如何解决这个问题。

【问题讨论】:

    标签: php mysql search pagination


    【解决方案1】:

    是的,没错,您必须将搜索关键字存储在会话变量中,然后您可以在任何页面中获取关键字,并在此基础上运行您的查询,希望这会对您有所帮助

    【讨论】:

    • TY。最终添加了以下代码。当然在顶部 session_start() 开始会话;我更改了 $construct = ''; 的第一个声明将其更改为 $construct = $_SESSION"["construct"]; 然后在关闭 if 语句之前的所有问题之后,我添加了 $_SESSION["construct"] = $construct; 我在整个站点中使用会话,所以我可以'不终止会话,因此我将其添加到初始搜索页面的顶部。$_SESSION["construct"] =''; 如果我不这样做,那么它将保存新搜索中最后设置的选项。跨度>
    猜你喜欢
    • 2018-08-17
    • 2017-04-11
    • 2020-05-19
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2018-06-02
    相关资源
    最近更新 更多