使用分页搜索过滤器?

时间:2017-04-25 14:54:35

标签: php mysql search pagination

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

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

我知道为什么会这样。这是因为我使用带有复选框的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> ";
        }
        }

例如,如果我搜索姓氏史密斯。它将显示10的结果。然后,当我按下下一个按钮时,它会出现我内置的错误,当它找不到搜索关键字的任何结果时。

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

我迷失了如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

是的你是的,你必须将你的搜索关键字存储在一个会话变量中,并且从那里你可以在任何页面获得关键字,并根据你必须运行你的查询希望这将有助于你