亲爱的朋友我有两个功能,也许我将来可能会有更多功能,但关键是我希望当用户根据相同文本字段上的邮政编码搜索酒店时功能 应该调用hotel_by_postel_code($ textvalue),当我根据国家/地区搜索该函数时 应该调用hotel_by_country($ textvalue)。以下是应显示结果的代码,但它根本不显示结果。
<?php require_once("includes/header.php");?>
<?php require_once("includes/connection.php")?>
<?php
if(isset($_POST['submit'])){
$message ="";
$textvalue = $_POST['search'];
if(empty($allhotels = hotel_by_postel_code($textvalue)) || empty($allhotels = hotel_by_country($textvalue))){
$message = "There is no record in the database";
}
}else{
$allhotels = select_all_hotels();
}
?>
<div class="cBoth"></div>
<div id="sep"></div>
<div id="mainContentSection" class="Calign">
<div id="detaillist">
<div id="searching" class="Calign">
<form action="list2.php" method="POST" id="searchForm">
<fieldset>
<input type="text" name="search" />
<input type="submit" name ="submit" value="Search" /></fieldset>
</form>
</div><!--End of searching-->
<?php
if(isset($message)){
echo"<div id=\"listtitle\">";
echo"<h2>".$message."</h2>";
echo"</div>";//End of listtitle div
}else{
echo"<div id=\"listtitle\">";
echo"<h2>Property Name</h2> <h2>Location</h2> <h2>Guest Rating</h2><h2>Hotel Rank</h2><h2>Per night</h2>";
echo"</div>";
}
?><!--End of listtitle-->
<div class="cBoth"></div>
<?php
$i=0;
while($hotels_set = mysql_fetch_array($allhotels)){
$room_rate = rateforhotel($hotels_set['hotel_id']);
if(!empty( $hotels_set['hotel_name']) && ($room_rate['hotel_id'] == $hotels_set['hotel_id'] ) ){
if($i % 2 == 0) { echo "<div id=\"innerlisteven\">"; }
else
{
echo"<div id=\"innerlistodd\">";
}
echo"<h2><a href =\"#\">". $hotels_set['hotel_name'] ."</a></h2>";
echo"<h2>". $hotels_set['country'] ."</h2>";
if(!intval($hotels_set['star'])){
echo"<h2>". $hotels_set['star'] ."</h2>";
}else{
echo"<h2>". $hotels_set['star'] . "<img src=\"img/repetimg/star.png\"/></h2>";
}
echo"<h2>". $hotels_set['star'] . "</h2>";
echo"<h2>". $room_rate['rate'] . "</h2>";
echo"</div>";
$i++;
}//end of if()
}//end of hotel while
mysql_close($con);
?>
</div><!--End of details-->
<div id="advertlisting">
<div id="search">search menu</div>
</div><!--End of adverts left-->
</div><!--End of end of maincontent-->
<div class="cBoth"><!-- clear Both--></div>
<?php require_once("includes/footer.php"); ?>
以下代码是函数本身
function hotel_by_country($country){
global $connection;
$query = "SELECT * FROM Hotels WHERE country ='{$country}'";
$hotel_set = mysql_query($query,$connection);
confirm_query($hotel_set);
return $hotel_set;
}
function hotel_by_postel_code($postal){
global $connection;
$query = "SELECT * FROM Hotels WHERE hotel_postal_code ='{$postal}'";
$hotel_set = mysql_query($query,$connection);
confirm_query($hotel_set);
return $hotel_set;
}
function select_all_hotels(){
global $connection;
$query = "SELECT *
FROM Hotels";
$hotel_set = mysql_query($query,$connection);
confirm_query($hotel_set);
return $hotel_set;
}
答案 0 :(得分:0)
你错过了)
函数的结束(右)括号empty
- 一个在OR之前,一个在条件结束时)。
变化:
if(empty($allhotels = hotel_by_postel_code($textvalue) OR empty($allhotels = hotel_by_country($textvalue))
为:
if(empty($allhotels = hotel_by_postel_code($textvalue)) OR empty($allhotels = hotel_by_country($textvalue))){
接下来,我会将这一行分成几行:
$allhotels1 = hotel_by_postel_code($textvalue);
$allhotels2 = hotel_by_country($textvalue);
echo "allhotels1 = $allhotels1; allhotels1 = $allhotels1\n";//for debug
if(empty($allhotels1) OR empty($allhotels2){...
还有最后一件事 - 您确定它应该是OR
而不是AND
吗?
答案 1 :(得分:0)
if(is_numermic($textvalue))
hotel_by_postel_code($textvalue)
else
hotel_by_country($textvalue)
答案 2 :(得分:0)
我认为您的问题在于此代码
if(empty($allhotels = hotel_by_postel_code($textvalue)) || empty($allhotels = hotel_by_country($textvalue))){
$message = "There is no record in the database";
}
首先,您使用$allhotels
填充hotel_by_postel_code($textvalue)
。然后用$allhotels = hotel_by_country($textvalue)
覆盖它。
此外,您应该将||
替换为&&
,因为只有当两个函数都没有返回任何值时才需要该消息。
<强>尝试强>
$allhotels = hotel_by_postel_code($textvalue);
if(empty($allhotels)) {
$allhotels = hotel_by_country($textvalue);
if(empty($allhotels)) {
$message = "There is no record in the database";
}
}
答案 3 :(得分:-1)
经过多次跟踪和错误后,我能够找到解决方案,以下是我发现的最佳选择,也许将来某人我会遇到同样的问题。要记住的一件事是IF和Else中的选项可以很容易地移动到单独文件中的函数中。我还添加了一个Html菜单来选择我搜索的选项类型,在这种情况下它是过滤器菜单。
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');?>
<?php require_once("includes/header.php");?>
<?php require_once("includes/connection.php")?>
<?php
if(isset($_POST['search']) && $_POST['search']!= "") {
if($_POST['filter'] == "HotelName"){
$search = $_POST['search'];
$sqlquery = "SELECT * FROM Hotels WHERE hotel_name LIKE '%$search%' OR description LIKE '%$search%'";
$allhotels = mysql_query($sqlquery, $connection) or die("Sorry Something wrong".mysql_error());
}else if($_POST['filter'] == "country"){
$search = $_POST['search'];
$sqlquery = "SELECT * FROM Hotels WHERE country LIKE '%$search%' OR hotel_address LIKE '%$search%'";
$allhotels = mysql_query($sqlquery, $connection) or die("Sorry Something wrong".mysql_error());
}else if($_POST['filter'] == "Postal"){
$search = $_POST['search'];
$sqlquery = "SELECT * FROM Hotels WHERE hotel_postal_code LIKE '%$search%'";
$allhotels = mysql_query($sqlquery, $connection) or die("Sorry Something wrong".mysql_error());
}
}
?>
<div class="cBoth"></div>
<div id="sep"></div>
<div id="mainContentSection" class="Calign">
<div id="detaillist">
<div id="searching" class="Calign">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" id="searchForm">
<fieldset>
<input type="text" name="search" />
<select name="filter">
<option value="HotelName">Hotel Name</option>
<option value="country">Country</option>
<option value="Postal">Postal Code</option>
</select>
<input type="submit" name ="submit" value="Search" />
</fieldset>
</form>
</div><!--End of searching-->
<div id="listtitle">
<h2>Property Name</h2> <h2>Location</h2><h2>Hotel Rank</h2><h2>Guest Rating</h2><h2>Per night</h2>
</div><!--End of listtitle-->
<div class="cBoth"></div>
<?php
if(!isset($allhotels)){
$allhotels = select_all_hotels();
}
$i=0;
while($hotels_set = mysql_fetch_array( $allhotels)){
$room_rate = rateforhotel($hotels_set['hotel_id']);
if(!empty( $hotels_set['hotel_name']) && ($room_rate['hotel_id'] == $hotels_set['hotel_id'] ) ){
if($i % 2 == 0) { echo "<div id=\"innerlisteven\">"; }
else
{
echo"<div id=\"innerlistodd\">";
}
echo"<h2><a href=\"desti_list.php?details=" . urlencode($hotels_set["hotel_id"]).
"\">" . $hotels_set['hotel_name'] ."</a></h2>";
echo"<h2>". $hotels_set['country'] ."</h2>";
if(!intval($hotels_set['star'])){
echo"<h2>". $hotels_set['star'] ."</h2>";
}else{
echo"<h2>". $hotels_set['star'] . "<img src=\"img/repetimg/star.png\"/></h2>";
}
echo"<h2>". $hotels_set['star'] . "</h2>";
echo"<h2>". $room_rate['rate'] . "</h2>";
echo"</div>";
$i++;
/*
echo"<h3><a href=\"desti_list.php?details=" . urlencode($hotels_set["hotel_id"]).
"\">Find Out More</a></span></h3>";
*/
}//end of if()
}//end of hotel while
mysql_close($connection);
?>
</div><!--End of details-->
<div id="advertlisting">
<div id="search">search menu</div>
</div><!--End of adverts left-->
</div><!--End of end of maincontent-->
<div class="cBoth"><!-- clear Both--></div>
<?php require_once("includes/footer.php"); ?>