我有一个用PHP和MySQL开发的旅游搜索应用程序。搜索功能正常。但我需要协助我的查询。搜索功能使用三个参数:Region,Country和Duration。根据用户选择的内容显示正确的结果。
我使用的PHP代码和MySQL结构允许我只为特定的游览分配一个国家,地区或持续时间。如果我要添加多个国家/地区,区域或持续时间,则会显示两个值均未找到任何游览。例如,如果在两个国家/地区列出了一个名为Indochina 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>
答案 0 :(得分:0)
explode(',',*your country field value*)
希望这有帮助。