0

maybe from the title its looks like this is a duplicated question, but I've checked all other question and no one is like mine, my problem is that I've a mysql query on Codeigniter like so :

    function getApplication($product_id){
      $this->db->from('tapplicationproduct')
                ->where('nProduct',$product_id)
                ->join('tapplication','tapplication.nApplication=tapplicationProduct.nApplication');

              $query = $this->db->get();
              $ret['rows'] = $query->result();
              $ret['number'] = $query->num_rows();

              return $ret;
    }

and here's the the view :

<div class="col-md-3">              
    <select name="manufact" onchange="this.form.submit()">
    <option value="" selected="selected">Selectioner Constructeur</option>
    <?php
      foreach ($app as $row) {
      $ManName = $Product_model->getManName($row->nManufacturer);
      echo "<option value='".$row->nSerie."'>".$ManName."</option>";
      }
    ?>  
    </select>
</div>

The problem is that for some products I got duplicated entries :

Problem Demonstration

I've tried the mysql instruction GROUP BY to group the results but the problem is that I need all the $row->nSerie to filter the next section of the results.

What I mean is that when a user clicks on the Constructor a second Select tag will appear with the Series of the relevant Constructor

I've done this but I can't figure out how to remove the duplicated entries and keep the ability to access their $row->nSerie I hope I can find some help about this problem, and thanks to every on in advance.

9
  • You can probably use DISTINCT to get unique data. Commented Aug 24, 2015 at 20:07
  • ^ or you can use a GROUP BY Commented Aug 24, 2015 at 20:08
  • @SariRahal but this can't solve my problem Commented Aug 24, 2015 at 20:09
  • 1
    If you just need manufacurers why are you querying through the product model... use your manufacturer model. Commented Aug 24, 2015 at 20:09
  • 1
    If you need the user to be able to select a particluar nSerie value, then you actually do need to display every one. I would suggest selecting a different column to add to the text portion of the <option> to disambiguate the records. Commented Aug 24, 2015 at 20:19

2 Answers 2

1

You can use Distinct before get().

 function getApplication($product_id){
      $this->db->from('tapplicationproduct')
                ->where('nProduct',$product_id)
                ->join('tapplication','tapplication.nApplication=tapplicationProduct.nApplication');
              $this->db->distinct();
              $query = $this->db->get();
              $ret['rows'] = $query->result();
              $ret['number'] = $query->num_rows();

              return $ret;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

and how can I access the nSerie of each instance?
Nothing has been changed when adding that line
maybe because are not the same results in row, you can use groupBy nManufacturer
I've used it but it only display the first one, and I can't get the other nManufacturer of the other rows to use them in the second part of the form
0

Ths solution is to use the array_unique() php function, since the mysql results returned by the query is an array then we can use array_unique() to eliminate the duplicated values like so :

<select name="manufact" onchange="this.form.submit()">
<option value="" selected="selected">Selectioner Constructeur</option>
<?php
  $apps = array_unique($app);
  foreach ($apps as $row) {
  $ManName = $Product_model->getManName($row->nManufacturer);
  echo "<option value='".$row->nSerie."'>".$ManName."</option>";
  }
?>  
</select>

And then store the keys that are going to be in the second pat of the form like so :

foreach ($app as $row) {
    echo "<input name='napp[]' value='".$row->nApplication."' type='hidden'>";
}

Now when the form is submitted you can the napp Array in the Where condition of the MySQL query

Comments

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.