phpcodeignitermaxquery-builder

Return the highest value from CodeIgniter model method using query builder


I want to know how to return the highest value in the array?

MY MODEL CODES

$this->db->select('additional');
$this->db->from('order_detail');
$this->db->where('order_id',$id);
$query = $this->db->get();
foreach ($query->result() as $row)
{
    $return[] = $row->additional;
}
return $return;

Solution

  • Just use select_max in query to get highest value and use row() to fetch single row without using foreach loop as

    $this->db->select_max('additional AS max_value');
    $this->db->from('order_detail');
    $this->db->where('order_id', $id);
    $query = $this->db->get();
    $ret = $query->row();
    return $ret->max_value;