Here's the 'Create Table' stuff. I've shortened it by removing most of the very similar lines creating the date columns. For simplicity, and to avoid slowing PHPMyAdmin too much, I've only created 'January', but it's enough to test out the Search system.
PHP Code:
CREATE TABLE `avail_2013` ( `room_id` decimal(7,2) NOT NULL, `member_id` int(4) NOT NULL, `type` varchar(6) COLLATE utf8_bin NOT NULL, `2013-01-01` varchar(6) COLLATE utf8_bin DEFAULT 'free', `2013-01-02` varchar(6) COLLATE utf8_bin DEFAULT 'free',...[many similar lines inserting dates 3-29 January]... `2013-01-30` varchar(6) COLLATE utf8_bin DEFAULT 'free', `2013-01-31` varchar(6) COLLATE utf8_bin DEFAULT 'free', UNIQUE KEY `room_id` (`room_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
'type' (the third column) means type of room, and options are currently 'double', 'twin', 'single', 'family', abbreviated to 'dbl', 'twn', 'sgl', 'fam' respectively.
Every room has it's own record (row) on the database. It's status (shown in the appropriate date column) may be either 'free' or 'booked', a further condition of 'provisional' ('prov') will be introduced when I get further into this.
There's a form where the user selects the type and number of rooms required. On submission the information from this form is used to create four search queries, of which the following are typical examples:
Double rooms:
Code:
SELECT room_id, member_id FROM avail_2013 WHERE type = 'dbl' AND `2013-01-01` = 'free' AND `2013-01-02` = 'free'
After PHP processing, the result is an array showing (as keys) the ID of those properties that have rooms available for the given dates, and the number of rooms (as values)
Result: Array ( [2043] => 3 [2008] => 1 [2066] => 2 )
Twin Rooms:
Code:
SELECT room_id, member_id FROM avail_2013 WHERE type = 'twn' AND `2013-01-01` = 'free' AND `2013-01-02` = 'free'
Result: Array ( [2043] => 2 [2008] => 1 [2066] => 3 )
Single Rooms:
Code:
SELECT room_id, member_id FROM avail_2013 WHERE type = 'sgl' AND `2013-01-01` = 'free' AND `2013-01-02` = 'free'
Result: Array ( [2043] => 1 [2066] => 1 )
Family Rooms:
Code:
SELECT room_id, member_id FROM avail_2013 WHERE type = 'fam' AND `2013-01-01` = 'free' AND `2013-01-02` = 'free'
Result: Array ( [2073] => 1 )
All the above arrays are stored as sub-arrays. $freelisting['dbl'], $freelisting['twn'], $freelisting['sgl'], $freelisting['faml']
If the user selects only (say) double and twin rooms, the Single and Family room queries won't run (or if they do, will give blank results). This leads to trouble later.
Now there's further PHP processing to compare the number(s) of rooms available to the numbers required, eliminating those properties that don't have sufficient rooms. All that works fine, though I'm sure there are other ways to do it.
Finally I want to know which properties have ALL the required rooms available which means finding out which IDs are included in ALL the arrays. This is where 'array_intersect' came in.
Code:
$finalresult = array_intersect($freelistings['dbl'], $freelistings['twn'],$freelistings['sgl'],$freelistings['fam']);
But it fails if any of the sub-arrays aren't present. I can't create dummy sub-arrays, because they wouldn't contain the right property IDs, so the search would fail (or be wrong). I need a way of searching those sub-arrays that are present, and finding those property IDs that are common to them all. Perhaps a 'foreach' loop, but I can see it getting complex.
Bookmarks