如何使用codeigniter计算唯一列的总和(列名是总计)

时间:2016-06-10 03:49:00

标签: php mysql codeigniter

控制器

这是控制器页面。

public function index() {
$data = array(
     'year' => $this->year_model->list_all(),
     'speces' => $this->speces_model->list_all(),
       );
          foreach ($data['year'] as $value):

                $temp = array();
                $temp['year_key'] = $value['year-range'];
                $temp['year_id'] = $value['id'];


                $temp['speces_key'] = array();
                foreach ($data['speces'] as $value1):

                    $temp['speces_key'][$value1['name']] = $this->db->get_where('expendituredata', array('spacesId' => $value1['id'], 'yearId' => $value['id']))->row_array();

                endforeach;
                $final_result[] = $temp;

            endforeach;
            $data['result'] = $final_result;
            $this->load->view('site/template', $data);
        }

模型

public function list_all() {
        return $this->db->get('year')->result_array();
    }

查看

这是查看文件。

<table>
 <thead>
       <tr>
          <th>Year Range</th>
          <?php foreach ($speces as $value): ?>
            <th> <?php echo $value['name']; ?></th>
           <?php endforeach; ?>
            <th>TOTAL</th>
             </tr>
             </thead>
         <tbody>
                <?php foreach ($result as $value): ?>
                  <?php $sum = 0; ?>
                    <tr>
                     <td><?php echo $value['year_key']; ?></td>
                <?php foreach ($value['speces_key'] as $key => $value2): ?>
                  <td><?php echo $value2['data']; ?></td>
                     <?php $sum+= $value2['data']; ?>
                        <?php endforeach; ?>

                    <td><?php echo $sum; ?></td>
                   </tr>
          <?php endforeach; ?>

            <tr>
               <td>Total</td>
                  <?php foreach ($speces as $value6): ?>
  <?php
  $this->db->select_sum('data');
  $query = $this->db->get_where('expendituredata', array('spacesId' => $value6['id']))->row_array();

 $totals = $query['data'];                                      
     ?>
      <td><?php echo $totals; ?></td>    
      <?php endforeach; ?>
  </tr>
</table>

[Screen Shot]

我的问题:如何计算列的总和(列名称总计)。列名称总计显示在屏幕截图上方。如何计算列的总和(列名称总计)和上面屏幕截图中红色箭头的总和显示

2 个答案:

答案 0 :(得分:0)

我认为你想要计算不是数据库字段的总列数之和。所以要做到这一点,你可以采用一个变量来加入每一行的列值。

只需将变量初始化为零。

<?php $sum = $totalSum = 0; ?>

现在为每一行添加sum变量的值。 改变你的路线,

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

<td><?php $totalSum+=$sum; echo $sum; ?></td>

现在,您的$totalSum字段将包含总列数的总和。你可以在任何你想要的地方展示它。

已更新 在视图文件中使用它并检查。

    <table>
     <thead>
           <tr>
              <th>Year Range</th>
              <?php foreach ($speces as $value): ?>
                <th> <?php echo $value['name']; ?></th>
               <?php endforeach; ?>
                <th>TOTAL</th>
                 </tr>
                 </thead>
             <tbody>
                     <?php $totalSum=0; ?>
                    <?php foreach ($result as $value): ?>
                      <?php $sum = 0; ?>
                        <tr>
                         <td><?php echo $value['year_key']; ?></td>
                    <?php foreach ($value['speces_key'] as $key => $value2): ?>
                      <td><?php echo $value2['data']; ?></td>
                         <?php $sum+= $value2['data']; ?>
                            <?php endforeach; ?>

                        <td><?php $totalSum+= $sum; echo $sum; ?></td>
                       </tr>
              <?php endforeach; ?>

                <tr>
                   <td>Total</td>
                      <?php foreach ($speces as $value6): ?>
      <?php
      $this->db->select_sum('data');
      $query = $this->db->get_where('expendituredata', array('spacesId' => $value6['id']))->row_array();

     $totals = $query['data'];                                      
         ?>
          <td><?php echo $totals; ?></td>    
          <?php endforeach; ?>
<td><?php echo $totalSum; ?></td>    
      </tr>
    </table>

我认为这可以解决您的问题。

答案 1 :(得分:0)

尝试使用

 <?php $sum = $sum + $value2['data']; ?>

而不是

 <?php $sum+= $value2['data']; ?>