我需要以不同的方式指定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个不同的其他语句,这看起来很复杂。
我使用置换和组合数学公式将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 :(得分: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();
}