php - 有关 PHP 和 MySQL 函数的帮助

标签 php mysql

我有一个用 PHP 和 MySQL 开发的旅游搜索应用程序。搜索功能运行正常。但我需要针对我的疑问提供帮助。搜索功能使用三个参数:地区、国家和持续时间。根据用户的选择,它会显示正确的结果。

我使用的 PHP 代码和 MySQL 结构允许我为特定旅行仅分配一个国家、地区或持续时间。如果我添加多个国家、地区或持续时间,这两个值都会显示“未找到旅游”。例如,如果名为:Indochina Tour 的旅游在两个国家/地区列出:柬埔寨和泰国,并且有人搜索柬埔寨的旅游,则印度支那旅游不会显示。当有人用泰国搜索时也是如此。但是,如果该旅行团仅列在一个国家(例如柬埔寨)下,那么当有人搜索柬埔寨旅行团时,它就会显示。

我希望任何旅行都有多个国家、地区或持续时间。如果一个 Tour 说 Tour Africa 列在两个国家/地区,id 分别为 6 和 9。我会将这两个国家/地区都放在国家/地区列中作为 6,9,并且在搜索时可以获得正确的结果。当我搜索国家/地区 ID 6 或 9 时,应该会出现该游览

这是我的database table

这是我的 PHP 代码:

<?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="searchtest1.php" method="post" target="_blank">
            <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>
                <option value="2">10 Days</option>
            </select>
        </div>
        <input type="submit" name="submit" value="submit" />
        </form>
    </body>

最佳答案

  • 据我了解,在您的示例中,“国家/地区”字段具有值(value) 6,9。
  • 但是当您从国家/地区下拉列表中选择一个选项时,例如埃塞俄比亚 值为 6。
  • 那么您的变量 $country 的值为 6。
  • 但在您所在的领域,该值是 6,9。
  • 因此,当您执行 where country='$country' 时,结果将类似于 6,9=6。
  • 因此,您想首先使用 PHP-explode 爆炸国家/地区字段, 这里将是explode(',',*您的国家/地区字段值*)

希望这有帮助。

关于php - 有关 PHP 和 MySQL 函数的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25399563/

相关文章:

mysql - 按优先级排序评论

mysql - 如何在格式化时间基础上添加 2 小时

php - 如何使用调试、TS 为 PHP 5.3.6 编译 php 扩展

php - 是否应该使用 PHP 打印页面的所有 HTML?

javascript - Php 计时器没有停止在 00 :00

mysql - 从一个巨大的表中获取随机值集

php - MySQL查询和子查询

php - PHP中的坐标旋转

mysql - 如何合并 2 个 mysql 查询

php - 如何使用 php 链接电子邮件地址