返回模型中的最高值以查看

时间:2016-07-25 10:15:53

标签: php codeigniter model-view-controller

我想知道如何比较这两个输出http://prntscr.com/bx7ay9并返回value中的最高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;

4 个答案:

答案 0 :(得分:1)

只需在查询中使用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;

答案 1 :(得分:0)

如果您想获得最大数量,请使用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');一起使用您的代码来获取数组中的最大数量。< / p>

答案 2 :(得分:0)

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

答案 3 :(得分:0)

刚刚使用了Codeigniter Query Bulder Class的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.