0

親愛なる友人 私には 2 つの関数があり、将来的にはさらに多くの関数を使用する可能性がありますが、ポイントは、ユーザーが同じテキスト フィールドの郵便番号に基づいてホテルを検索するときに、関数 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;
 }
4

4 に答える 4

0

)関数の閉じ(右)括弧がありません-1emptyつはORの前、もう1つは条件の終わりです)。
変化する:

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){...

最後にもう1つ、そうあるべきでORあり、そうではないということを確信していANDますか?

于 2012-07-26T05:40:59.627 に答える
0
if(is_numermic($textvalue))
   hotel_by_postel_code($textvalue)
else
  hotel_by_country($textvalue)
于 2012-07-26T05:50:01.557 に答える
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";
    }
}
于 2012-07-26T06:14:05.497 に答える
-1

多くの試行錯誤の後、私は解決策にたどり着くことができました. 心に留めておくべきことの 1 つは、IF と Else のオプションを別のファイルの関数に簡単に移動できることです。また、検索するオプションのタイプを選択するための Html メニューをもう 1 つ追加しました。この場合はフィルター メニューです。

<?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"); ?>
于 2012-07-29T00:01:56.677 に答える