Php 为不同的搜索选项调用不同的函数
亲爱的朋友,我有两个功能,将来可能会有更多功能,但问题是,当用户在同一个文本字段中根据邮政编码搜索酒店时,我希望使用该功能 应该调用hotel_by_postel_代码($textvalue),当我基于国家搜索时,则调用函数 应按国家/地区致电酒店($textvalue)。下面是应该显示结果的代码,但是它根本不应该显示结果Php 为不同的搜索选项调用不同的函数,php,Php,亲爱的朋友,我有两个功能,将来可能会有更多功能,但问题是,当用户在同一个文本字段中根据邮政编码搜索酒店时,我希望使用该功能 应该调用hotel_by_postel_代码($textvalue),当我基于国家搜索时,则调用函数 应按国家/地区致电酒店($textvalue)。下面是应该显示结果的代码,但是它根本不应该显示结果 <?php require_once("includes/header.php");?> <?php require_once("includes/conn
<?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"); ?>
您缺少空
函数的右括号(右括号),——一个在OR前面,一个在条件末尾)更改:
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){...
最后一件事-你确定它应该是
或而不是和?我认为你的问题在于这个代码
if(is_numermic($textvalue))
hotel_by_postel_code($textvalue)
else
hotel_by_country($textvalue)
if(empty($allhotels = hotel_by_postel_code($textvalue)) || empty($allhotels = hotel_by_country($textvalue))){
$message = "There is no record in the database";
}
首先,将$allhotels
填入hotel\u by\u postel\u代码($textvalue)
。然后用$allhotels=hotel\u by\u 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";
}
}
经过多次尝试和错误,我终于找到了解决方案,下面是我发现的最好的选择,也许将来有人会遇到同样的问题。需要记住的一点是,IF和Else中的选项可以轻松地移动到单独文件中的函数中。我还添加了一个Html菜单来选择我要搜索的选项类型,在本例中,它是filter菜单
<?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"); ?>
或者| |这不是原因,因为我尝试了这两种方法,但仍然不起作用。@SalimAlmughairi我不理解您的最后一条评论记录正在被调用,但问题是选择其中一个,而不是两个,或者什么都并没有。若我使用两个变量,当我想显示这意味着如果var1是while(var1=fetch…)和while(var2=fetch…),则每个条件必须有2个while循环。你看,我这样做的方式是,如果其中一个有效,那么它仍然适用于相同的,而贝娄。感谢您的支持,其中没有一个是数字,一个是字母数字,另一个是文本。我正在检查第一个是否为空,然后将第二个分配给同一个varibel。因此,您认为可能存在什么问题。问题是,即使第一个变量不为空,您也会覆盖该变量。我在回答中添加了一些代码。
$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";
}
}
<?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"); ?>