需要解决的问题:

假设一个用户选择了日期范围来进行订房,例如: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_idroom_idunitpriceroomsbookedroomstodayadminidcreatedate
int(11) primary key auto_incrementint(11)varchar(20)varchar(20)int(11) default 0int(11) default 0varchar(20)int(11)varchar(20)

ps截图:

酒店订房系统:如何使用mysql来确定一个时间段内的房间都是可订的-LMLPHP

 <?php

 function filter_rooms($check_in,$check_out,$room_ids){
$departure_days = (strtotime($check_out) - strtotime($check_in))/(24*3600);
$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";
//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
$room_ids_period = array();
$data = array($check_in,$check_out);
$count = SQL_select($sql,$data);
$length = count($count);
foreach($count as $value){
if($value['have_days'] == $departure_days){
$room_ids_period[] = $value['room_id'];
}
}
if($room_ids_period){
return implode(',',$room_ids_period);
}else{return '';}
} ?>

房间资料表(zroom)

room_idtitle_chstitle_chttitle_engstypehotel_idpricreatedatelastdatesummary_chssmummary_chtsummary_engpriceextrabedcondition_chscondition_chtcondition_engmaxroomslogourlcorperate
int(10)UNSIGNEDPRIMARYKEYAUTO_INCREMENT varcahr(50)varchar(50)varchar(50)smallint(5)int(10)smallint(5)varchar(20)varchar(20)medium textmedium textmediun textvarchar(45)int(11)varchar(1000)varchar(1000)varchar(1000)int(10)varchar(200)tinyint(1)

ps: zroom structure

酒店订房系统:如何使用mysql来确定一个时间段内的房间都是可订的-LMLPHP

05-11 20:11