[eluser]someoneinomaha[/eluser]
Hello.
I'm working on a band calendar that has the following structure:
show information (date, description, etc)
venue information (a show has one venue)
band information (a show can have multiple bands)
The method I'm using right now is to pull back all of the records within a certain date range - left joining the bands. This results is duplicate rows because there can be many bands to a show.
So what I've opted to do is return the rows from the query and build an array. That way in the view, I can loop through the array without removing duplicates. I think this will work just fine, but I'm wondering if this is a really inefficient way to do it.
My model method looks like this:
Code:
function get_music_shows($start_date, $end_date, $status_id)
{
$return_value = array();
$sql = 'SELECT s.*, v.id as venue_id, v.name as venue_name, b.id as band_id, b.name as band_name
FROM shows s
INNER JOIN venues v on s.venue_id = v.id
LEFT JOIN bands_shows bs on s.id = bs.show_id
LEFT JOIN bands b on bs.band_id = b.id
WHERE (s.show_date >= ? AND s.show_date <= ?) AND (s.showtype_id = 1) AND (s.status_id = ?)
ORDER BY s.show_date';
$query = $this->db->query($sql, array($start_date, $end_date, $status_id));
$result = $query->result();
$show_id = 0;
foreach($result as $row)
{
if ($show_id != $row->id)
{
$show_id = $row->show_id;
$row_value = array('show_id' => $row->id, 'venue_id' => $row->venue_id, 'cost' => $row->cost, 'ages' => $row->ages,
'time' => $row->time, 'details' => $row->details, 'show_date' => $row->show_date,
'venue_name' => $row->venue_name, 'venue_id' => $row->venue_id);
$return_value[$show_id] = $row_value;
}
$return_value[$show_id]['bands'][] = array('band_id' => $row->band_id, 'band' => $row->band_name);
}
return $return_value;
}
If anyone has feedback on this, I'd really appreciate it.
Thanks for your time.