2

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 error message is:

A PHP Error was encountered - Severity: Notice - Message: Array to string conversion

My MODEL:

public function af_am_sum()
{
    $this->db->select_sum('af_am_msm');
    $query = $this->db->get('non_clinical_total_tests');
    return $query->result();
}

My CONTROLLER:

public function index()
{
    $data['af_am_total'] = $this->Page_model->af_am_sum();

    $data['pages'] = $this->Page_model->get_list();

    $this->template->load('admin', 'default', 'district1', $data);

}

My VIEW:

<td><?php echo $af_am_total; ?></td>

1 Answers1

1

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

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • Ok, and so this will also change my variable within my Controller, and View files from $af_am_total to $my_sum correct? (I'm a super codeigniter noob) – Roberto Raymon Dec 06 '19 at 17:48
  • no, nothing else changes, its just a way to get the row value of the sum easier, in the line `return $query->row()->my_sum;` the variable $af_am_total will not change, as you continue to use in your controller `$data['af_am_total'] = $this->Page_model->af_am_sum();`, see also https://codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view, that explains how to get dynamic data into the view – Vickel Dec 06 '19 at 17:51
  • Amazing! It works. Thank you so much. I've taught myself the basics of codeigniter, but still get stumped from time to time with arrays vs variables vs objects. I'm a hands-on learner though and work better by building applications with tutorials. When I hear something and then do it, it makes sense... just very hard to find proper tutorials. – Roberto Raymon Dec 06 '19 at 18:50
  • the codeigniter docs are a great resource, one more hint, debug, debug, debug... you can use echo `'
    ';print_r($your_array);die;` to halt and show your array structure anywhere in your controller or model. Btw.: On Stack Exchange sites the way to say *thank you* is to 
    [upvote helpful answers and accept the most helpful](https://stackoverflow.com/help/someone-answers) by clicking the tick mark on the left. You're free to switch later when a better answer comes along.
    .
    – Vickel Dec 06 '19 at 19:00
  • Gotcha! Next I will research subtracting the sum of one column from another column, and dividing the sum of one column by the sum of another column! – Roberto Raymon Dec 06 '19 at 19:31
  • 1
    @Vickel Here is our new room https://chat.stackexchange.com/rooms/101945/room-for-u10-forward-and-vickel – U13-Forward Dec 08 '19 at 08:41
  • @vickel - I have more insight on this app here if you'd like to contribute... https://stackoverflow.com/questions/59968370/codeigniter-append-rows-in-view-then-add-those-rows-to-database-table – Roberto Raymon Feb 02 '20 at 20:28