2013-07-05 77 views
1

我需要指定6個不同的和不同的搜索過濾器以不同的方式來執行使用PHP在mysql數據庫中進行搜索。我很困惑如何去做。如何使用PHP中的過濾器執行搜索?

我正在一個網站,我需要進行非常大的搜索與6個不同的搜索過濾器。

這似乎對我來說很複雜,因爲這是我第一次用這個複雜的邏輯編寫這麼多的代碼。

這是HTML代碼:

<input type='text' name='searchterm' /> 

<select name='ind' class='custom-select'> 
    <option value='0'> Select Industry</option> 
    <option value='1'> Real Estate </option> 
    <option value='2'> Hospitality/Hotel/Tourism/Travel & Aviation </option> 
    <option value='3'> Financial Services/Banking </option> 
</select>   

<select name='spec' class='custom-select'> 
    <option value='0'> Select Specialization</option> 
    <option value='1'> Accounting/Banking & Finance/Insurance </option> 
    <option value='2'> Administration/Management/Executive </option> 
    <option value='3'> Architecture/Construction/Civil </option> 
</select>   

<select name='loc' class='custom-select'> 
    <option value='0'> Select Location</option> 
    <option value='1'> Lagos </option> 
</select> 

完整的HTML代碼是在這裏:http://jsbin.com/otibel/1/

有6個不同的選擇框,並各自爲搜索過濾器。

用戶只能選擇一個選擇框,一次搜索或選擇兩個,或一次選擇兩個搜索過濾器,依此類推。

我設法做搜索過濾器只要6,這個代碼是當用戶只選擇一個搜索過濾器。

function performSearchWithFilter(){ 
    if (($_GET['ind'] > 0) && (isset($_GET['ind'])) && ($_GET['spec'] <= 0) && ($_GET['loc'] <= 0) && ($_GET['workexp'] <= 0) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] <= 0)){ 
     //it will carry out the search, when just the ind select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] > 0) && isset($_GET['spec']) && ($_GET['loc'] <= 0) && ($_GET['workexp'] <= 0) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] <= 0)) { 
     //it will carry out the search, when just the spec select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] <= 0) && ($_GET['loc'] > 0) && (isset($_GET['loc'])) && ($_GET['workexp'] <= 0) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] <= 0)) { 
     //it will carry out the search, when just the loc select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] <= 0) && ($_GET['loc'] <= 0) && ($_GET['workexp'] > 0) && isset($_GET['workexp']) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] <= 0)) { 
     //it will carry out the search, when just the workexp select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] <= 0) && ($_GET['loc'] <= 0) && ($_GET['workexp'] <= 0) && ($_GET['type'] > 0) && isset($_GET['type']) && ($_GET['qualfctn'] <= 0)) { 
     //it will carry out the search, when just the type select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] <= 0) && ($_GET['loc'] <= 0) && ($_GET['workexp'] <= 0) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] > 0) && isset($_GET['qualfctn'])) { 
     //it will carry out the search, when just the qualfctn select box has been selected 
    } 
} 

我知道我可以這樣編寫搜索代碼,但它必須是25個不同的else語句,而且看起來很複雜。

我使用的排列與組合數學式爲具有佈置在算法這樣的6個搜尋參數:

ind, spec, loc, workexp, type, qualfctn, ind & spec, ind & loc, ind & workexp, ind & type, ind & qualfctn, spec & loc, spec & workexp, spec & type, spec & qualfctn, loc & workexp, loc & type, loc & qualfctn, workexp & type, workexp & qualfctn, type & qualfctn, ind & spec & loc & workexp & type & qualfctn, ind & spec & loc & workexp & type, ind & spec & loc & workexp, ind & spec & loc. 

這是我提出的搜索過濾器算法,即,如果用戶選擇的搜索過濾器 以不同的方式。

PHP中的哪些搜索插件,框架或Librbary可以爲我執行此操作,使用多個篩選器進行搜索並最大限度地減少我編寫的代碼量,因此我不必開始編寫大塊代碼或重新創建輪子再一次,我真的很困惑該怎麼做,以及如何做到這一點。

+0

獲取改變這些默認選項'<選項值= '0'>選擇工業'到''和PHP代碼中使用'isset($ _ GET [ 'IND'])'測試它。這樣它看起來更乾淨。 –

+0

烏鴉我不明白,你能解釋一下嗎? –

+0

你正在尋找一個SQL字符串生成器模式,基本上。 –

回答

1

一個有點冗長的代碼來讀取所有的變量,但一旦他們被讀取,一個簡單的查詢語句來運行您的搜索。我已經根據註釋中的解釋對數據庫表的結構做了一些假設,並且自然需要初始化數據庫連接。

// this code assumes that $link has been initialized as a mysqli object 
// with database connection open. 
// assuming database table name is "people" 
// assuming database table column names match the names of GET variables 
// if any of these assumptions are incorrect, you'll need to modify the 
// code to match the DB 

// initialize WHERE conditions 
$conditions = "1=1"; 

// test for ind 
if(isset($_GET['ind']) && ($_GET['ind'] > 0)) { 
    $conditions .= " AND ind='".$link->real_escape_string($_GET['ind'])."'"; 
} 

// test for spec 
if(isset($_GET['spec']) && ($_GET['spec'] > 0)) { 
    $conditions .= " AND spec='".$link->real_escape_string($_GET['spec'])."'"; 
} 

// test for loc 
if(isset($_GET['loc']) && ($_GET['loc'] > 0)) { 
    $conditions .= " AND loc='".$link->real_escape_string($_GET['loc'])."'"; 
} 

// test for workexp 
if(isset($_GET['workexp']) && ($_GET['workexp'] > 0)) { 
    $conditions .= " AND workexp='".$link->real_escape_string($_GET['workexp'])."'"; 
} 

// test for type 
if(isset($_GET['type']) && ($_GET['type'] > 0)) { 
    $conditions .= " AND type='".$link->real_escape_string($_GET['type'])."'"; 
} 

// test for qualfctn 
if(isset($_GET['qualfctn']) && ($_GET['qualfctn'] > 0)) { 
    $conditions .= " AND qualfctn='".$link->real_escape_string($_GET['qualfctn'])."'"; 
} 

// make sure we have at least one condition 
if(!$first) { 
    if(!$result = $link->query(" 
     SELECT * 
     FROM people 
     WHERE ".$conditions 
    )) { 
     // your error handling here 
    } 

    // read the results here 

    $result->free(); 
} 
+0

你可以跳過你的答案中的所有'$ first'邏輯,並在查詢中添加WHERE 1 = 1(因爲這是一個不斷的評估,它不會有任何性能)。然後,您可以使用'$ conditions。=「AND ...'來滿足您的所有條件,這將使您的代碼更具可讀性。 – s1lv3r

+0

非常棒的建議!謝謝:) – Zeeb