首页 文章

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

提问于
浏览
0

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

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

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

  • 如果某人选择了一个只有另外两个作为全选的区域,它将仅显示该区域下列出的那些游览 . 与国家和持续时间相同 .

  • 如果有人仅选择两个参数,如国家和持续时间,第三个选项为全选,则仅显示在所选国家和持续时间下列出的那些旅行 . 与地区和持续时间相同 .

这是我的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 "
"; echo "Tour Detail: $tour_detail"; echo "
"; echo "
"; echo "
"; } } else{ echo "No Tour Found"; echo "
"; echo "
"; } } ?> <!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索引选项为“全部”,而不是“选择” .

    例如 .

    区域选择

    <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 "
    "; echo "Tour Detail: $tour_detail"; echo "
    "; echo "
    "; echo "
    "; } } else{ echo "No Tour Found"; echo "
    "; echo "
    "; } } ?>

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

    <?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 "
    "; echo "Tour Detail: $tour_detail"; echo "
    "; echo "
    "; echo "
    "; } } else{ echo "No Tour Found"; echo "
    "; echo "
    "; } } ?> <!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

  • 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);
    
  • 0

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

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

    如果(isset($ _ POST [ '提交'])){

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

    对于Ex . 如果Region = Select All,则在您的查询中删除region ='$ region'

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

    希望那种有道理 .

相关问题