我再次为我的 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
Total Rate: 42.00 --> 这不包括第一晚的价格,因为晚上的数量从第二晚开始计算
结果 2
酒店:城市客栈
晚:4
入住时间:2012-09-01
退房时间:2012-09-05
Total Rate: 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
我将不胜感激任何能引导我完成项目的帮助。
非常感谢。