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

在PHP和MySQL中使用搜索功能的帮助,php,mysql,search,Php,Mysql,Search,我正在开发一个旅游网站,我可以选择允许我的用户根据地区、国家和持续时间搜索旅游 目前,我开发的搜索选项允许在所有搜索参数匹配的情况下搜索旅行并显示结果。例如,如果在“区域-非洲、国家-埃塞俄比亚”和“持续时间-5天”下列出了一次旅行,我必须选择所有参数以显示该旅行。但我想修改搜索选项,以便: 对于区域、国家和持续时间,将有一个全选选项。 如果有人使用“全选”选项搜索所有三个 它将显示数据库中所有可用的旅行 如果有人仅选择一个区域,而其他两个区域为“全选”,则该区域将被删除 仅显示该区域下列出的

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

目前,我开发的搜索选项允许在所有搜索参数匹配的情况下搜索旅行并显示结果。例如,如果在“区域-非洲、国家-埃塞俄比亚”和“持续时间-5天”下列出了一次旅行,我必须选择所有参数以显示该旅行。但我想修改搜索选项,以便:

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

  • 如果有人仅选择一个区域,而其他两个区域为“全选”,则该区域将被删除 仅显示该区域下列出的旅行团。同样的 有国家和期限

  • 如果某人仅选择两个参数,如国家/地区和持续时间 第三个选项为全选,它将只显示那些 在所选国家/地区和持续时间下列出。地区也一样 和持续时间

这是我的

我现在使用的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>

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

$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);

我不是PHP高手,但我想首先在你的HTML表单中,你会想要一个“全选”(除非这是你所说的“全选”的意思)

如果您确实选择了“全选”Value=“0”,则在您使用

如果(isset($_POST['submit'])){

然后需要if语句来检查任何值为“0”的字段。如果字段设置为0,则需要删除数据库查询的该部分

例如,如果Region=Select All,则在查询中删除Region='$Region'

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


希望这是有意义的。

有一些可能遵循您的代码

您可以将selects上的0索引选项设置为“All”,而不是“Select”

例如

区域选择

<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 />";
        }
    }
?>

BYP试验
区域
全部的
东南亚
非洲
欧洲
美国
澳大利亚
国家
全部的
柬埔寨
泰国
越南
缅甸
老挝
埃塞俄比亚
法国
纽约市
墨尔本
期间
全部的
5天

我注意到您的选择是相互依赖的。这意味着您需要在修改选择后更新cascade中的当前选项。要能够执行级联,您可以参考StackOverflow上的其他问题:

我认为这不是问题,只是您尚未开发的内容。您好,谢谢我编辑了su对象。这不是问题。我需要帮助开发它,因为我是PHP开发新手。谢谢。这正是我想要的。非常感谢:)再问一个问题。如果我在一次旅行中添加多个地区、国家或持续时间,搜索将不起作用。例如,一次旅行ABC有两个国家(CDE和XYZ)。在这两种情况下,当我使用CDE或XYZ进行搜索时,都会显示未找到巡更。有什么解决方案吗?这取决于您定义表注册表的方式和检索信息的方式。最好是使用另一个表来标识您的巡更可能与不同的国家相关。例如,byp_Tour_countries有两个字段:tour_id和country_id。通过这种方式,您可以从byp_tour表中删除国家代码,并在新表中执行选择。您的选择如下:
select*from byp_tour where tour id in(select tour_id from byp_tour_country where country_id=“$country”)
您需要将where行从以下位置替换为:
$whereClause=$whereClause.”和country='country'”;
替换为
$whereClause=$whereClause.”和tour\u id in(从byp\u tour\u country中选择tour\u id,其中country\u id='$country');
非常感谢。这很有帮助:)
<?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>