1

我需要以不同的方式指定 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 可以为我做到这一点,使用多个过滤器进行搜索,并最大限度地减少我编写的代码量,因此我不必开始编写这么大的代码块,或者重新发明轮子再次,我真的很困惑该做什么以及如何去做。

4

1 回答 1

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();
}
于 2013-07-05T06:35:03.490 回答