Ide*_*a13 4 php mysql codeigniter
我正在研究酒店预订系统,目前我正在尝试选择可用的房间(未预订)。
Rooms DB Structure:
ID
ROOM NAME
CAPACITY
HOTEL RESERVATIONS DB STRUCTURE:
ID
CHECK_IN
CHECK_OUT
ROOMS
...
Run Code Online (Sandbox Code Playgroud)
这是我当前的代码:
function searchFreeRooms($data){
$check_in = $data['fields']['check_in'];
$check_out = $data['fields']['check_out'];
$this->db->select("*");
$this->db->from('core_hotel_rooms');
$this->db->where("id NOT IN (select rooms,total_guests from res_hotel where check_in <= '$check_in' AND check_out >= '$check_in' OR check_in <= '$check_out' AND check_out >= '$check_out' OR check_in >= '$check_in' AND check_out <= '$check_out' ) ");
$query = $this->db->get();
return $query->result();
}
Run Code Online (Sandbox Code Playgroud)
用户可以一次预订许多房间,并且保留的房间ID存储在“ ROOMS”列中,并用逗号分隔,例如:2,3,5
在我前面,不应显示此列中存在的房间,但是我遇到了麻烦,因为仅选择了逗号前的第一个id(房间),例如:2,3,5>仅选择了2,而仍然选择了3,5显示在我的前面。
问题在这里: $this->db->where("id NOT IN (select rooms,total_guests from res_hotel where check_in <= '$check_in' AND check_out >= '$check_in' OR check_in <= '$check_out' AND check_out >= '$check_out' OR check_in >= '$check_in' AND check_out <= '$check_out' ) ");
我尝试了此方法:$this->db->where("id NOT IN (1, 2) ");它完美地工作,但第二查询没有上层方法。
对不起我的英语不好...
非常感谢所有能提供帮助的人!
考虑使用括号
$this->db->where("id NOT IN (select rooms,total_guests from res_hotel where (check_in <= '$check_in' AND check_out >= '$check_in') OR (check_in <= '$check_out' AND check_out >= '$check_out') OR (check_in >= '$check_in' AND check_out <= '$check_out' ) ) ");
Run Code Online (Sandbox Code Playgroud)
最后,在您的所有帮助下,我解决了我的问题!
工作代码:
function searchFreeRooms($data){
$check_in = $data['fields']['check_in'];
$check_out = $data['fields']['check_out'];
$query1 = $this->db->query("select rooms from res_hotel where (check_in <= '$check_in' AND check_out >= '$check_in') OR (check_in <= '$check_out' AND check_out >= '$check_out') OR (check_in >= '$check_in' AND check_out <= '$check_out' )");
$query1_result = $query1->result();
$room_id= array();
foreach($query1_result as $row){
$room_id[] = $row->rooms;
}
$room = implode(",",$room_id);
$ids = explode(",", $room);
$this->db->select("*");
$this->db->from('core_hotel_rooms');
$this->db->where_not_in('id', $ids);
$query = $this->db->get();
return $query->result();
}
Run Code Online (Sandbox Code Playgroud)
非常感谢!
| 归档时间: |
|
| 查看次数: |
20097 次 |
| 最近记录: |