I need to query a database and return distinct values for columns. The query is in a foreach loop and varies based on an array.
$get_all_col_names = $this->db->list_fields($table_by_product);
//This will return "X_SIZE", "X_PRINT", "X_QTY"
Now I have a foreach which needs to get the distinct values of “X_SIZE”, “X_PRINT”, and “X_QTY” respectively.
foreach ($X_types as $X) {
$this->db->select($X);
$this->db->distinct();
$qX = $this->db->get($table_by_product);
return $qX->result_object();
}
The problem with this current setup is that it is only returning DISTINCT values for X_QTY which is the LAST array in the list.
I need distinct values returned for ALL keys in the array. How can I make this work? Thanks for the help.
The problem is your foreach will return after the first round in the loop, try the following: