CodeIgniter Forums
Use select_min in model function query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Use select_min in model function query (/thread-60896.html)



Use select_min in model function query - El Forum - 07-25-2014

[eluser]Unknown[/eluser]
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
Code:
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;
  endforeach;
  return $data;
}
Second function
Code:
function recomended_offers_price($recomended_offers)
{
  $data = array();
  
  //$this->db->select_min('week_price');
  
  $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;
   endforeach;
  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