[eluser]TheFuzzy0ne[/eluser]
I would probably do it something like this (
untested:
MODEL:
Code:
function get_club_types()
{
$this->db->select('club_types.id, club_types.name, COUNT(clubs.id) as count', FALSE);
$this->db->from('club_types');
$this->db->join('clubs', 'club_types.id = club_type_id', 'INNER');
$this->db->group_by('name');
$this->db->order_by('name');
$res = $this->db->get();
if ($res->num_rows() > 0)
{
return $res->result_array();
}
return FALSE;
}
function get_clubs_by_type($club_type_ids=array())
{
if (is_array($club_type_ids) && count($club_type_ids) > 0)
{
$this->db->select('id, name, club_type_id');
$this->db->from('clubs');
$this->db->where_in('club_type_id', $club_type_ids);
$this->db->order_by('name');
$res = $this->db->get();
if ($res->num_rows() > 0)
{
$res = $res->result_array();
$ret = array();
foreach ($res as $row)
{
$ret[$row['club_type_id']][] = $row['name'];
}
$res->free_result();
return $ret;
}
}
return FALSE;
}
CONTROLLER:
Code:
function overzicht()
{
$this->load->model('vereniging_model');
$data['club_types'] = $this->vereniging_model->get_club_types();
$club_type_ids = array();
foreach ($data['club_types'] as $club_type) { $ids[] = $club_type['id']; }
$data['clubs'] = $this->vereniging_model->get_clubs_by_type($ids);
$this->load->view('vereniging/overzicht', $data);
}
VIEW:
Code:
<?php foreach ($club_types as $club_type): ?>
<h4 class="club-overview-title"><?php echo $club_type['name'] ?> (<?php echo $club_type['count']; ?>)</h4>';
<ul class="club-overview clear">';
<?php foreach ($clubs[$club_type['id']] as $club): ?>
<li><?php echo anchor('', $club['name']); ?></li>';
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
I'm sure there's a lot of room for improvement, however.