需要解决的问题:
假设一个用户选择了日期范围来进行订房,例如:2014-04-25至2014-04-30 ,那么现在问题就出现,你必须要确认在这个时间段内某个房间是否都是有房间的,如果没有那么当然不能订。
我的解决思路是:
1.获取到用户的check_in_date&check_out_date,然后计算出需要住宿多少天;
2.查询在这个时间段内,对应某个酒店所有的所有房间类型,并且房间数大于0,以房间id(room_id)分组的记录数各是多少,并获取room_id(具体MYSQL语句:"SELECT count(*) as have_days,room_id FROM zroom_day_price WHERE today>=? AND today<? AND room_id IN (".$room_ids.") AND rooms>0 GROUP BY room_id");
3.使用php程式来比较1(住宿天数)&2(对应时间段中某个房间id的记录数)是否相等。相等,则将该房间的id保存到一个预先定义好的数组中,最后在循环结束后,将该数组返回,并根据该房间id数组取出对应的房间detail information。
每天房间价格表(zroom_day_price)结构(structure)
room_day_price_id | room_id | unit | price | rooms | bookedrooms | today | adminid | createdate |
int(11) primary key auto_increment | int(11) | varchar(20) | varchar(20) | int(11) default 0 | int(11) default 0 | varchar(20) | int(11) | varchar(20) |
ps截图:
1 <?php 2 3 function filter_rooms($check_in,$check_out,$room_ids){ 4 $departure_days = (strtotime($check_out) - strtotime($check_in))/(24*3600); 5 $sql = "SELECT count(*) as have_days,room_id FROM zroom_day_price WHERE today>=? AND today<? AND room_id IN (".$room_ids.") AND rooms>0 GROUP BY room_id"; 6 //SELECT COUNT( * ) FROM zroom_day_price WHERE today >= '2014-04-27' AND today <= '2014-04-30' AND room_id IN ( 266, 267, 268, 269, 270 ) AND rooms >0 GROUP BY room_id 7 $room_ids_period = array(); 8 $data = array($check_in,$check_out); 9 $count = SQL_select($sql,$data); 10 $length = count($count); 11 foreach($count as $value){ 12 if($value['have_days'] == $departure_days){ 13 $room_ids_period[] = $value['room_id']; 14 } 15 } 16 if($room_ids_period){ 17 return implode(',',$room_ids_period); 18 }else{return '';} 19 } 20 21 ?>
房间资料表(zroom)
room_id | title_chs | title_cht | title_eng | stype | hotel_id | pri | createdate | lastdate | summary_chs | smummary_cht | summary_eng | price | extrabed | condition_chs | condition_cht | condition_eng | maxrooms | logourl | corperate |
int(10)UNSIGNEDPRIMARYKEYAUTO_INCREMENT | varcahr(50) | varchar(50) | varchar(50) | smallint(5) | int(10) | smallint(5) | varchar(20) | varchar(20) | medium text | medium text | mediun text | varchar(45) | int(11) | varchar(1000) | varchar(1000) | varchar(1000) | int(10) | varchar(200) | tinyint(1) |
ps: zroom structure