Reputation: 29
i have a trouble when parsing sum of one of my column in database to view using codeigniter. It returns error:"array to string conversion", can you help me to solve this problem ? Thank You
This is the code :
Model
function get_total_invest(){
$query = $this->db->query("SELECT SUM(price) FROM purchase");
if($query->num_rows()>0) {
return $query->result();
}else{
return 0;
}
}
Controller
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);
}
View
<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>
Upvotes: 0
Views: 148
Reputation: 103
Try out this solution in model
Model
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; }
}
Upvotes: 1