【问题标题】:Assistance with a Search Function in PHP and MySQL协助 PHP 和 MySQL 中的搜索功能
【发布时间】:2014-10-09 09:54:44
【问题描述】:

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

目前,我开发的搜索选项允许搜索旅游并在所有搜索参数匹配时显示结果。例如,如果 Tour 列在 Region-Africa、Country-Ethiops 和 Duration- 5 Days 下,我必须选择所有参数才能显示 Tour。但我想修改搜索选项,以便:

  • 对于地区、国家和持续时间会有一个全选选项。 如果有人使用全选选项搜索所有三个 参数它将显示数据库中所有可用的游览。

  • 如果有人只选择了一个区域,而其他两个为全选,它 将仅显示该区域下列出的那些旅行。相同的 国家和时长。

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

这是我的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>

【问题讨论】:

  • 我认为这不是问题,只是您尚未开发的东西。
  • 嗨,谢谢我已经编辑了主题。这不是问题。我需要帮助开发它,因为我是 PHP 开发新手

标签: php mysql search


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    如果您执行了“全选”值 =“0”,那么在您检查表单提交后:

    if(isset($_POST['submit'])){

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

    例如。如果 Region = Select All 然后在您的查询中删除 region='$region'

    这样您就不会返回 0 结果,因为您的 Region 字段不应包含任何 0 区域。

    希望那是有道理的。

    【讨论】:

      【解决方案3】:

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

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

      例如

      地区选择

      <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>
      

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

      【讨论】:

      • 谢谢。这正是我想要的。非常感谢:)
      • 还有一个问题。如果我在游览中添加多个地区、国家或持续时间,则搜索不起作用。例如,Tour ABC 分配了两个国家(CDE 和 XYZ)。当我在这两种情况下都使用 CDE 或 XYZ 进行搜索时,它显示未找到巡回赛。有什么解决办法吗?
      • 这取决于您定义表的注册表的方式以及检索信息的方式。最好有另一个表来确定您的旅行可以与不同的国家/地区相关联。例如。 byp_tour_countries 有 2 个字段: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." and country='$country'"; 替换为 $whereClause = $whereClause." and tour_id in (select tour_id from byp_tour_country where country_id = '$country')";
      • 非常感谢。这很有帮助:)
      猜你喜欢
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2011-02-05
      相关资源
      最近更新 更多