如何在PHP中使用过滤器执行搜索?

如何在PHP中使用过滤器执行搜索?,php,search,Php,Search,我需要以不同的方式指定6个不同的搜索过滤器,以便使用php在mysql数据库中执行搜索。我不知道该怎么做 我工作的网站,我需要进行非常大的搜索与6个不同的搜索过滤器 这对我来说似乎很复杂,因为这是我第一次用这种复杂的逻辑编写如此大量的代码 这是HTML代码: <input type='text' name='searchterm' /> <select name='ind' class='custom-select'> <option value='0'&

我需要以不同的方式指定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>
这是我做的搜索过滤器算法,即如果用户选择搜索过滤器 以不同的方式


PHP中的什么搜索插件、框架或Librbary可以为我做到这一点,使用多个过滤器进行搜索,并最大限度地减少我编写的代码量,这样我就不必开始编写这么多代码,也不必重新发明轮子,我真的不知道该怎么做,怎么做。

读取所有变量的代码有点长,但一旦读取了变量,就需要一个简单的查询语句来运行搜索。我对注释中解释的数据库表的结构做了一些假设,当然您需要初始化数据库连接

// 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();
}

一段有点长的代码,用于读取所有变量,但一旦它们被读取,就需要一个简单的查询语句来运行搜索。我对注释中解释的数据库表的结构做了一些假设,当然您需要初始化数据库连接

// 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();
}

获取将那些默认选项
selectindustry
更改为
selectindustry
,并在PHP代码中使用
isset($\u Get['ind'])
对其进行测试。raven我不明白,你能解释更多吗?基本上,你正在寻找一个sql字符串生成器模式。把那些默认选项
Select Industry
改为
Select Industry
,并在PHP代码中使用
isset($\u Get['ind'])
对其进行测试。这样看起来更干净。raven我不明白,你能解释更多吗?基本上,你在寻找sql字符串生成器模式。你可以跳过答案中的所有
$first
-逻辑,只需将
其中1=1
添加到查询中(因为这是一个持续的评估,不会影响任何性能)。然后,您可以使用
$conditions.=”和…
作为所有条件,这将使您的代码更具可读性。这是一个很好的建议!谢谢:)您可以跳过答案中的所有
$first
-逻辑,只需将
其中1=1
添加到查询中(因为这是一个恒定的评估,不会影响任何性能)。然后,您可以对所有条件使用
$conditions.=”和…
,这将使您的代码更具可读性。非常好的建议!谢谢:)
// 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();
}