2021年3月4日星期四

In Codeigniter view file adding php string values the simplest way

I'm trying to add up these values within this string. I tried "array_sum" but it made the entire page show blank. I also tried "explode" but it still just showed the same values.

DATA TABLE

goal_af_am_msm | goal_year |      77              2020     0               2020     3500            2020     406             2020     60              2020     1               2020     155             2020     150             2020     130             2020     125             2020     77              2020     271             2020     1               2020     0               2020     1               2020     102             2020     102             2020     250             2020     146             2020     77              2020     204             2020     1               2020     5               2021     78              2021     23              2021     87              2021     12              2021     76              2021  

MODEL

  function goal()    {          $query = $this->db->get('goals_table');          return $query->result();          }  

CONTROLLER

$data['goal_data'] = $this->State_model->goal();  

VIEW

<?php foreach($goal_data as $_2020_goal) : ?>  <?php if($_2020_goal->goal_year == '2020') echo $_2020_goal->goal_af_am_msm; ?>  <?php endforeach; ?>   

ADDITIONAL VIEW TRIED

<?php $sum = array(); ?>  <?php foreach($goal_data as $_2020_goal) : ?>  <?php if($_2020_goal->goal_year == '2020') echo $_2020_goal>goal_af_am_msm; ?>  <?php $sum[] = $_2020_goal->goal_af_am_msm; ?>  <?php endforeach; ?>  <?php echo array_sum($sum); ?>  

ERROR

A PHP Error was encountered  Severity: Notice    Message: Use of undefined constant goal_af_am_msm - assumed 'goal_af_am_msm'  

ACTUAL DATA OUTPUT

77 0 3500 406 60 1 155 150 130 125 77 271 1 0 1 102 102 250 146 77 204 1  

DESIRED DATA OUTPUT

5836  
https://stackoverflow.com/questions/66485072/in-codeigniter-view-file-adding-php-string-values-the-simplest-way March 05, 2021 at 08:35AM

没有评论:

发表评论