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


How to fetch values from specific columns using while loop in 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

城市区域:城市区域1

输出应为:

结果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

如果能帮我完成我的项目,我将不胜感激。

非常感谢。

此处:

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,因此与第一个结果的比较是错误的,并且第一家酒店没有显示在结果中。

此条件

if($htl_name!==$first_hotel_found){

当$htl_name第一次是Hotel City Inn时(如果第一家酒店是Hotel Beach Inn),则为true。您应该打印$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'];