0

i am using CodeIgniter and i have a problem with duplicated name.

This is my Model:

function displayCategory($id){
    $this->db->distinct();
    $this->db->select('category');
    $this->db->where('avenu_id',$id);
    $result = $this->db->get('personal_closest')->result_array();
    return $result;
}

This is my Controller:

$shop['category'] = $this->personal_closest->displayAvenuCategory($id);

And this is the View:

<ul class="tag-list">
    <?php foreach ($avenuadmin as $key => $value) { ?>
    <li><a href="<?php echo base_url().'social/category/'.$value['category']; ?>"><i class="fa fa-tag"></i> <?php echo $value['category']; ?></a></li>
     <?php } ?>
</ul>

I am using distinct() function to prevent duplicate but is not working and my output is:

ACCESSORIES, HOME, TECH, WHATCHES, TECH, TECH. I need to show just ACCESSORIES, HOME, TECH, WHATCHES.

2
  • You show a Model with the method called displayCategory. Your Controller code snippet shows you are calling displayAvenueCategory to load a variable called $category and your View is using a variable called $avenueadmin. What is the real,actual situation? Commented Apr 27, 2015 at 9:44
  • As you can see the problem was in the View i was calling the wrong variable $avenuadmin instead $category ._.° Commented Apr 27, 2015 at 10:07

3 Answers 3

1

Distinct will not always work.

You should add ->

$this->db->distinct();
$this->db->group_by('column_name');

OR

$this->db->select('DISTINCT `name`'); 
$this->db->select('*');

In Controller

$data['category'] = $this->personal_closest->displayAvenuCategory($id);
$this->load->view('view_file_name', $data);

In View use

$category

to access the details.

Sign up to request clarification or add additional context in comments.

6 Comments

Same problem doesn't change nothing :(
Updated the answer, use group_by after distinct. Hope it helps.
Is not working, if i use distinct(); and grou_by(); i got the same output
where this $avenuadmin array come. submit your full code
If you have used -> $shop['category'] = $this->personal_closest->displayAvenuCategory($id); Then the view variable should be $category.
|
0

Try this:

function displayCategory($id){
    // $this->db->distinct();
    $this->db->select('category');
    $this->db->where('avenu_id',$id);
    $this->db->group_by('category'); // group by category name
    $result = $this->db->get('personal_closest')->result_array();
    return $result;
}

Comments

0

I have make a var_dump to the $shop['category']; and that's the results:

  array(4) {
  [0]=>
  array(1) {
    ["category"]=>
    string(3) "CAP"
  }
  [1]=>
  array(1) {
    ["category"]=>
    string(4) "HOME"
  }
  [2]=>
  array(1) {
    ["category"]=>
    string(4) "TECH"
  }
  [3]=>
  array(1) {
    ["category"]=>
    string(8) "WHATCHES"
  }
}

4 Comments

Then what is the problem . your array show category are distinct??
The problem is in the view i got duplicates.
Yes, the records are distinct here.
How that can be possible if you are using the right variable in view. Please check your variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.