使用PHP和MySQL中的搜索功能提供帮助

时间:2014-08-15 16:21:39

标签: php mysql search

我正在开发一个旅行和旅游网站,我可以选择允许我的用户根据地区,国家和持续时间搜索旅游。

目前,我开发的搜索选项允许搜索游览并在所有搜索参数匹配时显示结果。例如,如果巡回赛列在地区 - 非洲,国家 - 埃塞俄比亚和持续时间 - 5天,我必须选择所有参数来显示巡回赛。但我想修改搜索选项,以便:

  • 区域,国家和持续时间会有一个全选选项。 如果有人使用“全选”选项搜索所有三个选项 参数将显示数据库中可用的所有Tours。

  • 如果有人选择了一个Region,只有其他两个选择All All,那么 只会显示该区域下列出的那些旅行。相同 国家和持续时间。

  • 如果有人只选择两个参数,如Country和Duration with 第三个选项为全选,它只会显示那些游览 列出在所选国家和持续时间下。与地区相同 和持续时间。

这是我的Database Structure

我现在使用的PHP代码是:

<?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("byp");

    if(isset($_POST['submit'])){
        $region=$_POST['region'];
        $country=$_POST['country'];
        $duration=$_POST['duration'];

        $tour = mysql_query("select * from byp_tour where region='$region' and country='$country' and duration='$duration'");
        $tourNum = mysql_num_rows($tour);

        if($tourNum >0){

            while($result=mysql_fetch_array($tour)){

                $tour_name = $result['tour_name'];
                $tour_detail = $result['tour_detail'];

                echo "Tour Name: $tour_name";
                echo "<br />";
                echo "Tour Detail: $tour_detail";
                echo "<br />";
                echo "<br />";
                echo "<br />";
            }
        }
        else{
            echo "No Tour Found";
            echo "<br />";
            echo "<br />";
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>BYP Test</title>
    </head>
    <body>
        <form action="byptest.php" method="post">
            <div>
                <label>Region</label>
                <select id="region" name="region">
                <option value="0">Select</option>
                    <option value="1">South East Asia</option>
                    <option value="2">Africa</option>
                    <option value="3">Europe</option>
                    <option value="4">America</option>
                    <option value="5">Australia</option>                               
                </select>
            </div>
            <div>
                <label>Country</label>
                <select id="country" name="country">
                <option value="0">Select</option>
                    <option value="1">Cambodia</option>
                    <option value="2">Thailand</option>
                    <option value="3">Vietnam</option>
                    <option value="4">Myanmar</option>
                    <option value="5">Laos</option>
                    <option value="6">Ethiopia</option>
                    <option value="7">France</option>
                    <option value="8">New York City</option>
                    <option value="9">Melbourne</option>
                </select>
            </div>
            <div>
                <label>Duration</label>
                <select id="duration" name="duration">
                <option value="0">Select</option>
                    <option value="1">5 Days</option>

                </select>
            </div>
            <input type="submit" name="submit" value="submit" />
        </form>
    </body>
</html>

3 个答案:

答案 0 :(得分:0)

您必须根据设置的变量构建查询。你可以这样做:

$where = '';
if (!empty(($duration)) {
    $where = " AND duration='$duration'";
}
if (!empty($country)) {
    $where = " AND duration='$country'";
}
if (!empty($duration)) {
    $where = " AND duration='$duration'";
}
$where = substr($where, 5);
$sql = "select * from byp_tour" . ($where ? " WHERE $where" : '');
$tour = mysql_query($sql);

答案 1 :(得分:0)

我不是PHP高手,但我会首先在你的HTML表单中你想要一个&#34; Select All&#34; (除非你已经指的是&#34;选择&#34;)

如果你做了&#34;全选&#34;值=&#34; 0&#34;然后,在您使用

检查表单提交后

如果(isset($ _ POST [&#39;提交&#39;])){

然后你需要if语句来检查任何值为&#34; 0&#34;的字段。如果某个字段设置为0,那么您将希望删除该部分数据库查询。

对于Ex。如果Region = Select All,则在您的查询中删除region =&#39; $ region&#39;

这样你就不会返回0结果,因为你的Region字段不应该有0的任何区域。

希望有道理。

答案 2 :(得分:0)

有一些可能性可以遵循您的代码。

你可以让选择的0索引选项为“全部”,而不是“选择”。

E.g。

区域选择

<select id="region" name="region">
    <option value="0">Select</option>
    <option value="1">South East Asia</option>
    <option value="2">Africa</option>
    <option value="3">Europe</option>
    <option value="4">America</option>
    <option value="5">Australia</option>                               
</select>

将转变为

<select id="region" name="region">
    <option value="0">All</option>
    <option value="1">South East Asia</option>
    <option value="2">Africa</option>
    <option value="3">Europe</option>
    <option value="4">America</option>
    <option value="5">Australia</option>                               
</select>

另一种可能性是创建具有特定索引的新选项,例如9999;

通过这种方式,您可以修改查询,以便根据您的选择选择所有元素。

代码应修改为:

<?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("byp");

    if(isset($_POST['submit'])){
        $region=$_POST['region'];
        $country=$_POST['country'];
        $duration=$_POST['duration'];

        //define the index for the All option
        $optionAllValue = 0; //add here the option index value used for the 'All' option
        //define the where clause for the query
        //in order to avoid many conditions verifications, we start it as 1=1
        $whereClause = "1=1";

        //now we check if the option selected for each field is not the value defined for the option 'All'
        //this is just an example, and the best would be to create a function to avoid the replication of code 
        if($region != $optionAllValue)
        {
            $whereClause = $whereClause." and region='$region'";
        }
        if($country != $optionAllValue)
        {
            $whereClause = $whereClause." and country='$country'";
        }
        if($duration != $optionAllValue)
        {
            $whereClause = $whereClause." and duration='$duration'";
        }

        $query = "select * from byp_tour where ".$whereClause;

        //original query select * from byp_tour where region='$region' and country='$country' and duration='$duration'"
        $tour = mysql_query($query);
        $tourNum = mysql_num_rows($tour);

        if($tourNum >0){

            while($result=mysql_fetch_array($tour)){

                $tour_name = $result['tour_name'];
                $tour_detail = $result['tour_detail'];

                echo "Tour Name: $tour_name";
                echo "<br />";
                echo "Tour Detail: $tour_detail";
                echo "<br />";
                echo "<br />";
                echo "<br />";
            }
        }
        else{
            echo "No Tour Found";
            echo "<br />";
            echo "<br />";
        }
    }
?>

整个修改过的例子可以在下面找到:

<?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("byp");

    if(isset($_POST['submit'])){
        $region=$_POST['region'];
        $country=$_POST['country'];
        $duration=$_POST['duration'];

        //define the index for the All option
        $optionAllValue = 0; //add here the option index value used for the 'All' option
        //define the where clause for the query
        //in order to avoid many conditions verifications, we start it as 1=1
        $whereClause = "1=1";

        //now we check if the option selected for each field is not the value defined for the option 'All'
        //this is just an example, and the best would be to create a function to avoid the replication of code 
        if($region != $optionAllValue)
        {
            $whereClause = $whereClause." and region='$region'";
        }
        if($country != $optionAllValue)
        {
            $whereClause = $whereClause." and country='$country'";
        }
        if($duration != $optionAllValue)
        {
            $whereClause = $whereClause." and duration='$duration'";
        }

        $query = "select * from byp_tour where ".$whereClause;

        //original query select * from byp_tour where region='$region' and country='$country' and duration='$duration'"
        $tour = mysql_query($query);
        $tourNum = mysql_num_rows($tour);

        if($tourNum >0){

            while($result=mysql_fetch_array($tour)){

                $tour_name = $result['tour_name'];
                $tour_detail = $result['tour_detail'];

                echo "Tour Name: $tour_name";
                echo "<br />";
                echo "Tour Detail: $tour_detail";
                echo "<br />";
                echo "<br />";
                echo "<br />";
            }
        }
        else{
            echo "No Tour Found";
            echo "<br />";
            echo "<br />";
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>BYP Test</title>
    </head>
    <body>
        <form action="byptest.php" method="post">
            <div>
                <label>Region</label>
                <select id="region" name="region">
                    <option value="0">All</option>
                    <option value="1">South East Asia</option>
                    <option value="2">Africa</option>
                    <option value="3">Europe</option>
                    <option value="4">America</option>
                    <option value="5">Australia</option>                               
                </select>
            </div>
            <div>
                <label>Country</label>
                <select id="country" name="country">
                <option value="0">All</option>
                <option value="1">Cambodia</option>
                <option value="2">Thailand</option>
                <option value="3">Vietnam</option>
                <option value="4">Myanmar</option>
                <option value="5">Laos</option>
                <option value="6">Ethiopia</option>
                <option value="7">France</option>
                <option value="8">New York City</option>
                <option value="9">Melbourne</option>
            </select>
        </div>
        <div>
            <label>Duration</label>
            <select id="duration" name="duration">
                <option value="0">All</option>
                <option value="1">5 Days</option>
            </select>
        </div>
        <input type="submit" name="submit" value="submit" />
        </form>
    </body>
</html>

我注意到你的选择是相互依赖的。这意味着您需要在修改选择后更新级联中的当前选项。为了能够执行级联,您可以在StackOverflow上引用其他问题:cascading dropdowns from mysql with javascript and php