我正在开发一个旅游和旅游网站,我有一个选项,允许我的用户搜索基于地区,国家和期限的旅游。
目前,我开发的搜索选项允许搜索旅游和显示结果,如果所有的搜索参数匹配。例如,如果一个旅游列在区域-非洲,国家-埃塞俄比亚和持续时间-5天,我必须选择所有的参数,以显示巡回赛。但我想修改搜索选项,以便:
这是我的数据库结构
我现在使用的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>发布于 2014-08-15 16:57:44
在您的代码中有一些可以遵循的可能性。
您可以让“选择”上的“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>我注意到你们的选择是相互关联的。这意味着在Selects的on被修改之后,您需要在级联中更新当前的选项。要执行级联,您可以参考StackOverflow:带有javascript和php的mysql级联下拉列表上的其他问题
发布于 2014-08-15 16:28:57
您必须根据设置了哪些变量来构建查询。你可以这样做:
$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);发布于 2014-08-15 16:31:54
我不是PHP高手,但首先在HTML表单中,您需要一个"Select“(除非这就是您所说的”选择“的意思)
如果您执行了"Select“Value = "0”,那么在您检查表单提交后,使用您的:
if(isset($_POST‘’submit‘)){
然后,YOu将需要if语句来检查任何值为"0“的字段。如果字段设置为0,则需要删除数据库查询的该部分。
为了快递。如果区域=选择所有,那么在查询中删除区域=‘$ Region’
这样,您就不会返回0结果,因为您的“区域”字段不应该有任何区域为0。
希望这样说得通。
https://stackoverflow.com/questions/25330211
复制相似问题