【问题标题】:How do I perform search with filters in PHP?如何在 PHP 中使用过滤器执行搜索?
【发布时间】: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 可以为我做到这一点,使用多个过滤器进行搜索,并最大限度地减少我编写的代码量,因此我不必开始编写这么大的代码块,或者重新发明轮回,我真的很困惑该做什么以及如何去做。

【问题讨论】:

  • 将这些默认选项 &lt;option value='0'&gt; Select Industry&lt;/option&gt; 更改为 &lt;option&gt;Select Industry&lt;/option&gt; 并在 PHP 代码中使用 isset($_GET['ind']) 对其进行测试。这样看起来干净多了。
  • 乌鸦我不明白,你能解释一下吗?
  • 基本上,您正在寻找一种 sql 字符串构建器模式。

标签: php search


【解决方案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();
}

【讨论】:

  • 您可以跳过答案中的所有$first-logic,只需将WHERE 1=1 添加到查询中(因为这是一个持续的评估,它不会占用任何性能)。然后,您可以在所有条件下使用$conditions .= " AND ...,这将使您的代码更具可读性。
  • 好建议!谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2012-10-23
  • 2023-03-26
  • 2018-04-14
  • 2019-07-03
相关资源
最近更新 更多