Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}
?>
like image 743
Hobby99 Avatar asked Jun 02 '26 04:06

Hobby99


1 Answers

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

like image 136
Juris Malinens Avatar answered Jun 03 '26 21:06

Juris Malinens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!