CodeIgniter Forums

Full Version: Use select_min in model function query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hi all
I have a little problem using "$this->db->select_min" in a model function.
I have 2 functions in model: first must return all results if a clause is true (this works fine).
The second one must select and return the smallest value finded in database column table for each row from first function.

First function
function recomended_offers($id)
  $data = array();
  $this->db->where('id_locality =', $id);
  $this->db->where('recomended =', 'yes');
  $this->db->limit('8', '0');
  $query1 = $this->db->get('locations');
  foreach($query1->result() as $row1):
   $data[] = $row1;
  return $data;
Second function
function recomended_offers_price($recomended_offers)
  $data = array();
  $this->db->order_by("week_price", "desc");
  $query = $this->db->get('locations_price');
     //if ($query->num_rows() > 0)
           //$data[$row->id_location] = $row;
          // return $data;
  foreach ($query->result() as $row):
   $data[$row->id_location] = $row;
  return $data;
In table "locations_price" each location from first function have several price values coresponding. Unfortunely the use of order by DESC or ASC will not return to me the smallest value because the values are in random order inserted in this column and i had not any key in this column to use so order by statement return me the corect value.
The commented lines from seconf function I tried to use for selecting smallest value but with no luck