2012-07-26 98 views
0

亲爱的朋友我有两个功能,也许我可能有更多的未来,但重点是,我想当一个用户搜索酒店基于同一文本字段上的邮政编码功能 应该调用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

OR < - in if不是这个用法 - > || – swapnesh 2012-07-26 05:36:33

+0

@swapnesh是的。 – alfasin 2012-07-26 05:38:35

+0

这不是原因,因为我尝试了两个,它仍然无法正常工作。 – 2012-07-26 05:42:56

回答

-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"); ?> 
0

你缺少右(右)支架的empty功能) - 一个或之前,一个在条件的结尾) 。
变化:

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

+0

仍然没有解决 – 2012-07-26 05:45:17

+0

@SalimAlmughairi看到更新 – alfasin 2012-07-26 05:48:12

+0

邮政编码可以是字母数字,所以这将不会工作 – 2012-07-26 05:49:38

0
if(is_numermic($textvalue)) 
    hotel_by_postel_code($textvalue) 
else 
    hotel_by_country($textvalue) 
+0

没有一个是数字一个是字母数字,另一个是文本 – 2012-07-26 06:03:11

0

我认为你的问题就出在这代码

if(empty($allhotels = hotel_by_postel_code($textvalue)) || empty($allhotels = hotel_by_country($textvalue))){ 
    $message = "There is no record in the database"; 
} 

首先填写$allhotelshotel_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"; 
    } 
} 
+0

什么iam试图检查,如果第一个是空的,然后分配第二个相同的变量,所以你认为可能是什么问题。 – 2012-07-26 06:25:27

+0

问题是,即使第一个不为空,您也会覆盖该变量。我在答案中添加了一些代码。 – Fluitketel 2012-07-26 07:03:54