phpmysqlcodeigniteractiverecorddistinct

CodeIgniter's distinct() method behaves unexpectedly with `get_where()` versus `get()`


Whenever I use get_where() in the model, it gives me all database table entries including duplicates. When I use get() only it only gives me the first entry, but I want all distinct entries.

Controller = site.php:

public function western_australia()
{
    $this->load->model("continents_get");
    $data["results"] = $this->continents_get
                            ->getMaincategories("western_australia");
    $this->load->view("content_western_australia", $data);
}

Model = continents_get.php

function getMaincategories($western_australia)
{
    $this->db->distinct();
    $query = $this->db->get_where("p_maincategories", 
                    array("page" => $western_australia));
    return $query->result();
}

view = content_western_australia.php

<?php
foreach ($results as $row) {
    $page = $row->page;
    $main_en = $row->main_en;
    echo $main_en;
}
?>

Solution

  • Distinct will not always work. You should add ->group_by("name_of_the_column_which_needs_to_be unique");