Home Ask Login Register

Developers Planet

Your answer is one click away!

Saibamen February 2016

How to select free rooms for reservations in CodeIgniter - two tables?

I want to select free rooms between dateStart and dateEnd in CodeIgniter.

I have 2 tables - rooms and reservations.

My current model code:


$this->db->join('reservations', 'rooms.ID = reservations.RoomID', 'left outer');

$this->db->where('rooms.Size >=', $PeopleNumber);
$this->db->where('reservations.DateStart <=', $DateEnd);
$this->db->where('reservations.DateEnd >=', $DateStart);


$query = $this->db->get('rooms');

I've already checked this question: MySQL Looking for free rooms in reservation system

but I don't know how to multiselect in CodeIgniter.

My SQL Fiddle: http://sqlfiddle.com/#!9/c1f95/8


Saibamen February 2016

Ok i resolved this:

$this->db->where('`ID` NOT IN (SELECT `RoomID` FROM `reservations` WHERE `DateStart` < "' .$DateEnd. '" AND `DateEnd` > "' .$DateStart. '")', NULL, FALSE);

Post Status

Asked in February 2016
Viewed 3,302 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life