如何使用Codeigniter计算每列的总和 [英] How to Calculate Sum of each Columns Using Codeigniter

查看:354
本文介绍了如何使用Codeigniter计算每列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器



这是控制器页面。

 code> 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-& > get_where('spendingituredata',array('spacesId'=> $ value1 ['id'],'yearId'=> $ value ['id'])) - > row_array();

endforeach;
$ final_result [] = $ temp;

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

查看

这是查看文件。

 < table& 
< thead>
< tr>
< th>年范围< / 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>总计< / td>
< / tr>
< / table>

ScreenShot



我的问题:如何计算以上屏幕截图中的总计。



所以请查看上面的截图和Calculatre总计为所有动物根据年。

解决方案

尝试使用这样:

  $ select = array(
'table.col1',
'table.col2',
'table.COW + table.DOG + table.CAT + table.BIRD + table.ARV + table.CAMP as TOTAL'
);
$ this-> db-> select($ select);
$ this-> db-> from('spendingituredata');
$ this-> db->其中(array('spacesId'=> $ value1 ['id'],'yearId'=> $ value ['id'
$ this-> db-> row_array();

或使用这个简单的SQL查询



从表格中选择COW + DOG + CAT + BIRD + ARV + CAMP作为TOTAL,其中col ='val';


Controller

This is Controller Page.

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);
        }

View

This is View file.

 <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>
                </tr>
</table>

ScreenShot

MY Question : How to Calculate Total in above screen shot.

so please view above screenshot and Calculatre total for all animal according to year.

解决方案

Try to use something like this :

$select =  array(
                'table.col1',
                'table.col2',
                'table.COW+table.DOG+table.CAT+table.BIRD+table.ARV+table.CAMP as TOTAL'
               );  
$this->db->select($select); 
$this->db->from('expendituredata');   
$this->db->where(array('spacesId' => $value1['id'], 'yearId' => $value['id']));
$this->db->row_array();

Or use this simple SQL query

select COW+DOG+CAT+BIRD+ARV+CAMP as TOTAL from table where col = 'val';

这篇关于如何使用Codeigniter计算每列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆