0

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>
2
  • the error is pretty obvious, its an array to string conversion, the result is and array of objects and you are trying to echo that array and echo only for strings. Commented Aug 7, 2019 at 8:29
  • 1
    $this->item_model->get_total_invest() returns an array. Commented Aug 7, 2019 at 8:37

1 Answer 1

1

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; }
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.