如何在php / mysql中使用while循环从特定列中获取值?

时间:2012-09-01 21:55:03

标签: php mysql

我正在与我的PHP结构再次挣扎,你们给了我很多帮助。提前致谢。 现在我对自己有了新的挑战。我的数据库中有以下表格:

  id |     htl_name    |      city_zone     |   price    |   given_date
------------------------------------------------------------------------
  1  | Hotel Beach Inn |   Cityzone1        |  10.00     |   2012-09-01
  2  | Hotel Beach Inn |   Cityzone1        |  10.00     |   2012-09-02
  3  | Hotel Beach Inn |   Cityzone1        |  10.00     |   2012-09-03
  4  | Hotel Beach Inn |   Cityzone1        |  11.00     |   2012-09-04
  5  | Hotel Beach Inn |   Cityzone1        |  11.00     |   2012-09-05
  6  | Hotel City Inn  |   Cityzone1        |  15.00     |   2012-09-01
  7  | Hotel City Inn  |   Cityzone1        |  15.00     |   2012-09-02 
  8  | Hotel City Inn  |   Cityzone1        |  16.00     |   2012-09-03
  9  | Hotel City Inn  |   Cityzone1        |  16.00     |   2012-09-04
 10  | Hotel City Inn  |   Cityzone1        |  16.00     |   2012-09-05
-------------------------------------------------------------------------

当用户输入签到日期,签出日期并选择城市区域时,脚本将使用PHP / MySQL结构过滤所需的城市区域和输入期间内的日期。到目前为止这么好并且工作但让我们假装用户输入:

登记入住时间:2012-09-01

退房时间:2012-09-05

城区:Cityzone1

输出应为:

结果1

酒店:海滩旅馆

夜晚:4

登记入住时间:2012-09-01

退房时间:2012-09-05

总费率:42.00 - >这排除了第一晚的房价,因为夜晚的数量从第二晚开始计算

结果2

酒店:城市客栈

夜晚:4

登记入住时间:2012-09-01

退房时间:2012-09-05

总费率:67.00 - >这排除了第一晚的房价,因为夜晚的数量从第二晚开始计算

到目前为止,我已完成以下脚本:

//MySQL Select structure that apply filter on user's entered information


$filter1=mysql_query("SELECT * FROM.........  ORDER BY htl_name, city_zone, given_date LIMIT 1");
$filtern=mysql_num_rows($filter1);
...
if($filtern>=1){
  While($row=mysql_fetch_array($filter1){
     $first_hotel_found=$row['htl_name'];
  }

  // New filter but listing everything within the date period

  $new_filter=mysql_query("SELECT * FROM ........ORDER BY htl_name, city_zone, given_date");
  $final_price=0; 
  $output="";
  while($row=mysql_fetch_array($new_filter){
     $htl_name=$row['htl_name'];
     $price=$row['price'];

     $final_price=$final_price+$price;

     if($htl_name!==$first_hotel_found){
        output .='Hotel:'.$htl_name.'<br/>';
        ... NO WORRIES WITH NUMBER OF NIGHTS AND CHECK-IN / CHECK-OUT DATES
        output .='Total Rate:'.$final_price.'<br/>';
        //after output the result go back clean up the $final_price and start adding again for the new Hotel
        $final_price=0;
        //$first_hotel_found assumes the actual $row
        $first_hotel_found=$row['htl_name'];
      }

    } // End of the while

}else{
     echo "There are no rates available for the information entered!";
     }

}

.... HTML portion ....

<body>
  <?php echo $output; ?>
</body>
</html>

问题是输出没有列出两家酒店而是列出最后一家酒店的名称和第一家酒店的总费率:

酒店:城市客栈

夜晚:4

登记入住时间:2012-09-01

退房时间:2012-09-05

总费率:42.00

我很感激能帮助我完成项目的任何帮助。

非常感谢。

2 个答案:

答案 0 :(得分:1)

下面:

if($filtern>=1){
  While($row=mysql_fetch_array($filter1){
   $first_hotel_found=$row['htl_name'];
}

您正在设置$first_hotel_found下的第一家酒店以及稍后进入该循环的时间:

 while($row=mysql_fetch_array($new_filter){

你有if($htl_name!==$first_hotel_found)

由于上面你设置了$first_hotel_found这个比较,第一个结果是假的,第一个酒店没有显示在结果中。

答案 1 :(得分:1)

这个条件

if($htl_name!==$first_hotel_found){

恰好是$ htl_name首次是Hotel City Inn(如果第一家酒店是Hotel Beach Inn)。你应该打印$ htl_name而不是$ first_hotel_found。

您永远不会打印有关第二家酒店的信息,因为相同的条件将永远不再成立。如果您添加第三家酒店,那么第一次$ htl_name就是那家酒店的确如此。要解决这个问题,您可以将创建输出的代码复制到循环外部,以便上次执行它:

output .='Hotel:'.$htl_name.'<br/>';
... NO WORRIES WITH NUMBER OF NIGHTS AND CHECK-IN / CHECK-OUT DATES
output .='Total Rate:'.$final_price.'<br/>';
//after output the result go back clean up the $final_price and start adding again for the new Hotel
$final_price=0;
//$first_hotel_found assumes the actual $row
$first_hotel_found=$row['htl_name'];