2017-04-25 88 views
0

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

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

我有一个想法,为什么它的发生。这是因为我使用带复选框的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> "; 
     } 
     } 

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

我认为发生的事情是当下一个按钮被按下时,它改变GET url来显示页面2,但是当这发生时它会丢失搜索sql的值,因为它不会再看到$ SLastName isset。

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

回答

1

是你的权利,你必须存储您的搜索关键字在会话变量中,并从那里你可以在任何页面,那你必须运行查询的希望的基础上,这将帮助你

+0

TY获取关键字。结束添加下面的代码。 当然在顶部 开始会话session_start(); 我改变了第一个声明 $ construct =''; 更改为 $ construct = $ _SESSION“[”construct“]; 然后在关闭if语句之前的所有issets之后,我添加了 $ _SESSION [”construct“] = $ construct; 我使用会话该网站,所以我不能杀死会议,所以我把它添加到最初的搜索页面顶部 $ _SESSION [“construct”] =''; 如果我没有这样做,那么它会保存最后设置的选项在新的搜索。 – Markw