2017-07-15 24 views
1

我如何添加代码字表中的所有列(表名钱箱)?我有这个代码和这张表。添加列codeigniter

SQL:

cash_id incoming outgoing 

    1  215.55 55.22 

    2  58.69 4.88 

    3  100  20 

现在我需要从传入374.24和传出80,1共有,但我不为我的工作。

$data['cashbox'] = $this->db->query('SELECT SUM(incoming) FROM cashbox')->num_rows(); 

视图<?= $cashbox ?>

+0

为什么如果你想总共使用'num_rows'。使用'result'并更改'$ data ['cashbox'] = $ this-> db-> select_sum('incoming') - > result();'。它会返回'array'所以'$ data ['cashbox'] [0]' – urfusion

+0

嘿,谢谢你的回答。但我用这个代码得到这个“消息:调用未定义的方法CI_DB_mysqli_driver :: result()” –

+0

你也可以使用'$ this-> db-> get();'。 '$ query = $ this-> db-> select_sum('incoming','incoming'); $ query = $ this-> db-> get('cashbox'); $ result = $ query-> result();' – urfusion

回答

2

您需要更改声明是这样的:

$this->db->select_sum('incoming','incoming'); 
$this->db->from('cashbox'); 
$query = $this->db->get(); 
$data['cashbox'] = $query->row()->incoming; 
+0

非常感谢:) –

+0

但是现在我遇到了小数点位置 的问题。如果我添加一些数字,如0.15,3.73,25,28.80我得到这个fback 57.67999999999999。我怎么能让我只有xx.xx –

1

此代码应该帮助你。

$query = $this->db->select_sum('incoming', 'incoming'); 
$query = $this->db->get('cashbox'); 
$result = $query->result(); 
echo return $result[0]->incoming; 
1

在你的模型中试一试。我认为它有效。

$this->db->select_sum('incoming'); 
$this->db->from('your table name') 
$query = $this->db->get(); 
return $query->$result[0]->incoming;