2016-07-25 34 views
1

我想知道如何比较这两个输出http://prntscr.com/bx7ay9并返回中最高的array返回从模型中查看的最高值

MY型号代码

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

对于这样一个小输出例如,它可能是更好的只写的输出直接在这里不是发送的屏幕截图的。 – contradictioned

回答

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; 
+0

谢谢它帮助我很多。 – erinr

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');与您的代码来获取数组中的最大数。

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

只用笨查询工具的详情类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.