codeigniter - how to add up values in table column

前端 未结 1 959
闹比i
闹比i 2021-01-21 11:29

Trying to get the sum of the values entered within a column (af_am_msm) from my table (non_clinical_total_tests). I want to display that total within an html table.

My e

1条回答
  •  醉梦人生
    2021-01-21 12:04

    Codeigniter's result() function always returns an array of objects. In your view you echo it out as a string, hence the error. In order to avoid that you either produce result rows, looping through the array, or:

    like in your example, as you are expecting only one row (the sum of a column) you have the possibility to get this result row (without the need to loop through it) with row()->the_name_of_your_column. To simplify the output we can create an alias for the mysql sum, naming it as second parameter:

    $this->db->select_sum('af_am_msm','my_sum');
    $query = $this->db->get('non_clinical_total_tests');
    return $query->row()->my_sum;
    

    which echos in your view as pretended

    0 讨论(0)
提交回复
热议问题