过滤现有搜索结果

时间:2014-04-17 16:10:28

标签: php html mysql

我想创建一个能够根据用户规范以更窄的方式过滤一般搜索结果的搜索系统。

例如,用户首先执行场所的一般搜索,然后搜索系统列出该场所的所有用户,并且在下一步中,用户希望根据年龄过滤该用户列表。

以下代码会对来自特定位置的用户进行常规搜索,并且只有在搜索框中指定了关键字并且在提交之前选择了年龄组时,它才会根据年龄过滤搜索结果。

我想要实现的是,用户可以在提交常规搜索后立即过滤结果。例如,一般搜索结果是John,Mary,Robert,并且在下一步中用户选择年龄组18-25,系统立即过滤名称John。

非常欢迎任何想法如何做到这一点!

这是php代码:

<?php
include_once("php_includes/db_travel_vowel.php");

$search_output = "";
$search_output1 ="";
$username ="";
$age_form ="";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = preg_replace('#[^0-9a-z]#i', '', $_POST['searchquery']);

    $sqlCommand = "(SELECT * FROM users WHERE country LIKE '%$searchquery%' OR town LIKE '%$searchquery%' OR city LIKE '%$searchquery%')";

    $query = mysqli_query($cn, $sqlCommand) or die(mysqli_error($cn));
    $count = mysqli_num_rows($query);

    if($count >= 1){
        $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />";

        while($row = mysqli_fetch_array($query)){
            $url = $row["url"];
            $f_name = $row["f_name"];
            $l_name = $row["l_name"];
            $user = $f_name;
            $user.= "&nbsp;";
            $user.= $l_name;
            $username = $row["username"];

            if($_POST['age'] == "1" || $_POST['age'] == "2" || $_POST['age'] == "3" || $_POST['age'] == "4" || $_POST['age'] == "5"  ){
                $age=$_POST['age'];
                $sqlC = "(SELECT f_name, l_name, url FROM users WHERE username='$username' AND age_group='$age')";
                $query1 = mysqli_query($cn, $sqlC) or die(mysqli_error($cn));

                while($row1 = mysqli_fetch_array($query1)){
                    $url1 = $row["url"];
                    $f_name1 = $row["f_name"];
                    $l_name1 = $row["l_name"];
                    $user1 = $f_name;
                    $user1.= "&nbsp;";
                    $user1.= $l_name;
                    $username1 = $row["username"];
                    $search_output .= '<a href="'.$url1.'">'. $user1 .'</a><br>';
                }
            } else {
                $search_output .= '<a href="'.$url.'">'. $user .'</a><br>';
            }
        } // close while
    } else {
        $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
    }
}
?> 

HTML code:

<form id="search_new" action="index_search1.php" method="post">
    <input name="searchquery" type="text" size="70" maxlength="88" placeholder="Search for places and members..." />
    <input type="submit" />
    <div id="options">
        <select name="age">
            <option selected>Age group<option>
            <option value="1">18-28<option>
            <option value="2">29-39<option>
            <option value="3">40-50<option>
            <option value="4">51-61<option>
            <option value="5">62-more<option>
        </select>
    </div>
</form>

<?php echo $search_output; ?>

非常感谢!

0 个答案:

没有答案