Php 如何在codeigniter视图中找出特定字段的和

Php 如何在codeigniter视图中找出特定字段的和,php,mysql,codeigniter,Php,Mysql,Codeigniter,我想显示forloop中一个特定字段的总和,它来自数据库,这是我的代码内部视图 <?php foreach($exptype as $exptypes) : ?> <tr> <td><?php echo $exptypes->expensestype; ?></td> <?php $this->db->select_sum('amount');

我想显示forloop中一个特定字段的总和,它来自数据库,这是我的代码内部视图

<?php foreach($exptype as $exptypes) : ?>

    <tr>
        <td><?php echo $exptypes->expensestype; ?></td>
        <?php
          $this->db->select_sum('amount');
          $this->db->from('westline_expenses');
          $this->db->where('expensestype',$exptypes->expensestype);
          $this->db->where('headofexpense','TAX');

          $query = $this->db->get();
        ?>
        <?php foreach($query as $taxexp) : ?>
        <td><?php echo $taxexp; ?></td>
        <?php endforeach; ?>
    </tr>

<?php endforeach; ?>


但是上面的代码不起作用,有人能在这方面帮助我吗。非常感谢

您需要从运行的查询中获取结果集

$results = $query->result_array()
然后可以对结果集执行foreach

作为旁注,您需要更改
echo$taxexp
回显$taxexp[0]

试试看

foreach ($query->result() as $taxexp){
  echo $taxexp->amount;
}

试试这个:

我认为您正在使用此代码。此代码在控制器中使用,然后在视图中分配

<?php
    $this->db->select_sum('amount');
    $this->db->from('westline_expenses');
    $this->db->where('expensestype',$exptypes->expensestype);
    $this->db->where('headofexpense','TAX');
    $query = $this->db->get();
    $data = $query->result_array();
    echo($data[0]['amount']); 
?>

根据代码。。。