【发布时间】:2013-07-05 00:04:45
【问题描述】:
我需要以不同的方式指定 6 个不同且不同的搜索过滤器,以便使用 php 在 mysql 数据库中执行搜索。我很困惑如何去做。
我正在开发一个网站,我需要使用 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 中的 Search Plugin、Framework 或 Librbary 可以为我做到这一点,使用多个过滤器进行搜索,并最大限度地减少我编写的代码量,因此我不必开始编写这么大的代码块,或者重新发明轮回,我真的很困惑该做什么以及如何去做。
【问题讨论】:
-
将这些默认选项
<option value='0'> Select Industry</option>更改为<option>Select Industry</option>并在 PHP 代码中使用isset($_GET['ind'])对其进行测试。这样看起来干净多了。 -
乌鸦我不明白,你能解释一下吗?
-
基本上,您正在寻找一种 sql 字符串构建器模式。