我有一个用PHP和MySQL开发的旅游搜索应用程序。搜索功能正常工作。但我需要帮助来查询我的问题。搜索函数具有三个参数:区域、国家和持续时间。根据用户选择的内容,它显示了正确的结果。
我使用的PHP代码和MySQL结构只允许我为特定的旅游指定一个国家、地区或持续时间。如果我要添加多个国家、地区或持续时间,则显示这两个值都找不到旅游。例如,如果一个名为:印度支那之旅的旅游列在两个国家:柬埔寨和泰国,而有人在柬埔寨寻找旅游,那么印度支那旅游就不会出现。当有人在泰国搜查的时候也是一样。但是,如果这次旅行只列在一个国家的下面,比如柬埔寨,那么当有人搜索柬埔寨旅游时,它就会出现。
我想有多个国家,地区或持续时间的任何旅游。如果一次巡回赛说非洲之旅列在id 6和9的两个国家下面,我会把这两个国家放在国家栏中,作为6,9,并且在搜索时可以得到正确的结果。当我搜索国家id 6或9时,旅游应该出现。
这是我的数据库表
这是我的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>
发布于 2014-08-20 08:49:24
explode(',',*your country field value*)
希望这能有所帮助。
https://stackoverflow.com/questions/25399563
复制相似问题