2017-04-25 111 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