【问题标题】:Errror result Array to string conversion in codeigniter在codeigniter中错误结果数组到字符串转换
【发布时间】:2019-08-07 08:26:51
【问题描述】:

在解析数据库中我的一列的总和以使用 codeigniter 查看时遇到问题。它返回错误:“数组到字符串的转换”,你能帮我解决这个问题吗?谢谢你

这是代码:

型号

    function get_total_invest(){
    $query = $this->db->query("SELECT SUM(price) FROM purchase");
    if($query->num_rows()>0) {
        return $query->result();
    }else{
        return 0;
    }
}

控制器

public function index(){
    $d['user_session'] = $this->session->userdata('username');
    $d['total_invest'] = $this->item_model->get_total_invest();
    $this->load->view('dashboard_view', $d);

}

查看

          <div class="col mr-2">
                    <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Asset Invest</div>
                     <div class="h5 mb-0 font-weight-bold text-gray-800">IDR <?php echo $total_invest?></div>
            </div>

【问题讨论】:

  • 错误非常明显,它是一个数组到字符串的转换,结果是对象数组,您试图回显该数组并仅回显字符串。
  • $this-&gt;item_model-&gt;get_total_invest() 返回一个数组。

标签: php arrays codeigniter codeigniter-3


【解决方案1】:

在模型中试用此解决方案

型号

function get_total_invest(){
    $query = $this->db->query("SELECT SUM(price) TotalPrice FROM purchase");
   if($query->num_rows()>0) { return $query->result()[0]->TotalPrice ; }
   else{  return 0; }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2023-03-11
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多