I have a form on which I have a multiselect for different boxes and for boxes there exists some records i-e (the number of openings of the box) in the database but it might be possible that for a selected box no records exists ie (the box is not opened even single time). Currently when I select different boxes and if records does not exist for a box it does not return the empty array for that box I want to it return even the empty results for the selected box. My query is
public function getBoxOpenings($boxes, $from_date, $to_date){
$query = $this->db->select('box_id, COUNT(box_id) AS Openings')
->from('boxes')
->where_in('box_id', $boxes)
->where('actiontime >=', $from_date)
->where('actiontime <=', $to_date)
->group_by('box_id')
->get();
$data = $query->result_array();
return $data;
}
If I select 3 boxes and submit the form and records exists for only 2 boxes it returns something like this
Array
(
[0] => Array
(
[mc_boxes_idmc_boxes] => 12
[location] => FRA-Air
[Openings] => 1
)
[1] => Array
(
[mc_boxes_idmc_boxes] => 14
[location] => FRA-Hof
[Openings] => 1
)
)
How can I get all three records with one empty like this
[2] => Array
(
[mc_boxes_idmc_boxes] => 16
[location] => Test
[Openings] =>
)
How Can I get the records for all the selected boxes with empty arrays for those which have no records.
Thanks
The problem here is obvious: You only insert a row into that table when the box is opened by user. If the box not opened ever that table contains no record for that box.
If You then do a
select * from table where id in (1, 2, 3, 4, 5)but Your table contains only rows for ids 2 and 3, the result set will contain only the data for the ids 2 and 3 as there are no records found for other ids.I guess You only could call this query in a loop for each
$box_idfrom$boxes– when the data is found return it otherwise return zero. Like this:But this is not very nice as You do one query per each box that will be very resource consuming when having a lot of boxes…