从模型到视图返回最高值


Return the highest value from model to view

我想知道如何比较这两个输出http://prntscr.com/bx7ay9并返回CCD_ 2中最高的CCD_?

我的型号代码

$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;

只需在查询中使用select_max即可获得最高值,并使用row()获取单行,而不使用foreach loop作为

$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;
$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[] = max($row->additional);
    }
    return $return;

刚刚使用了Codeigniter查询Bulder类的select_max

型号,

public function get_max ($id){
    $this->db->select_max('additional');
    $this->db->where('order_id',$id);
    $query = $this->db->get('order_detail');
    return $query->row();
}

在控制器中,

$max = $this->Model->get_max($id);
echo $max['additional']; // Display and see the value. 

如果您想获得最大值,请使用select_max(),

$this->db->select_max('additional');
$this->db->from('order_detail');
$this->db->where('order_id',$id);
$query = $this->db->get();
if($query->num_rows()){// check if data is not empty
   return $query->row()->additional;
}
return false;// you can return false here

并且如果您想获得结果数组以供下次使用,那么您可以在代码中使用max()和$this->db->select('additional');来获得数组中的最大值。