php - 如何找出 View 中特定字段的总和 Codeigniter

标签 php mysql codeigniter

我想显示来自数据库的for循环内特定字段的总和,这是我在 View 中的代码

<?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; ?>

但是上面的代码不起作用,任何人都可以在这方面帮助我。非常感谢

最佳答案

尝试

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

关于php - 如何找出 View 中特定字段的总和 Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21015132/

相关文章:

mysql - 如果外键不为空,则左连接多个表 - mysql 优化

javascript - "bind"和 "binding"在 PHP 和 JavaScript 等语言中意味着什么?

php - 缓存和未缓存的日志查询

php - Opencart Checkout我的sql错误

mysql - SQL 中的多个 LIKE

Codeigniter native session 或 ci session 库

javascript - 在可滚动部分的视口(viewport)中,如何更改元素的样式属性?

php - 将数组从 Controller 传递到 codeigniter 中查看

php - Magento - 在分组产品表中显示自定义属性

PHP - MYSQL - 测试数据库服务器