我需要指定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'>選擇工業'到''和PHP代碼中使用'isset($ _ GET [ 'IND'])'測試它。這樣它看起來更乾淨。 –
烏鴉我不明白,你能解釋一下嗎? –
你正在尋找一個SQL字符串生成器模式,基本上。 –