How to SELECT unique rows using CodeIgniter's query builder
19:17 26 Jan 2015

My Database table looks like this (rough outline):

    +--------+-----------+-------------+---------+------------+------------+
    | log_id | host_name |   status    | host_id | profile_id | event_date |
    +--------+-----------+-------------+---------+------------+------------+
    |   1    |   site1   |   Online    |    2    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+
    |   2    |   site1   |   Online    |    2    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+
    |   3    |   site1   |   Offline   |    2    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+
    |   4    |   site2   |   Online    |    4    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+ 
    |   5    |   site2   | Maintenance |    4    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+

Here are my codes:

VIEW:

 
    ['host_name;?>',help_model->filter_online($id,$row->host_id, 'Online');?>,
    help_model->filter_online($id,$row->host_id, 'Offline');?>,
    help_model->filter_online($id,$row->host_id,'Maintenance');?>],
 

CONTROLLER:

public function userreports()
{       
    $data['id']=$this->session->userdata('profile_id');
    $this->session->set_userdata('from_date', '2015-01-01');
    $from_date = $this->session->userdata('from_date');
    $date = date('Y-m-d', now());
    $this->session->set_userdata('to_date', $date);
    $to_date = $this->session->userdata('to_date');
    $data['result'] = $this->help_model->all_logs($this->session->userdata('profile_id'));
    $this->load->view('squidtopus1-host-reports', $data);
}

MODEL:

function all_logs($id)
{
    $id = $this->session->userdata('profile_id');
    $this->db->select("*");
    $this->db->from('server_log');
    $this->db->where('profile_id',$id);
    $query = $this->db->get()->result();
    return $query;
}

function filter_online($data, $host_id, $status)
{
    $from_date = $this->session->userdata('from_date');
    $to_date = $this->session->userdata('to_date');
    $id = $this->session->userdata('profile_id');
    $this->db->select()->from('server_log');
    $this->db->where('profile_id', $id);
    $this->db->where('status', $status);
    $this->db->where('host_id', $host_id);
    $this->db->where('event_date >=', $from_date);
    $this->db->where('event_date <=', $to_date);
    $data =  $this->db->get()->result();
    $stat_count = 0;
    foreach ($data as $row) {
        $stat_count++;
    }

    return $stat_count;
}

The information that needs to displayed is shown properly, but the problem is that it repeatedly shows it for each row in the database that matches 'profile_id = $id'(in this case, profile_id=1). Ideally it should show:

['site1', 2,1,0], ['site2', 1,0,1]

But instead it shows:

['site1', 2,1,0],['site1', 2,1,0],['site1', 2,1,0],['site2', 1,0,1],['site2', 1,0,1]

I can't determine where I've gone wrong.

php codeigniter duplicates distinct query-builder