php - 如何在codeigniter中计算数据库中的行总数

标签 php mysql

我有一行名为“数量”的行,并且在此字段中有数量的数量。如何计算 codeigniter 中的数量总数并将其传递给查看? 我已从数据库表中获取“数量”行并在 for 循环中计算它们。

最佳答案

您可以使用select_sum查询生成器方法。

$total_quantity = 0;

$row = $this->db->select_sum('quantity', 'total_quantity')->get('your_table')->row();

if (isset($row)) {
    $total_quantity = $row->total_quantity;
}

$this->load->view('your_view', ['total_quantity' => $total_quantity]);

关于php - 如何在codeigniter中计算数据库中的行总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43642312/

相关文章:

MySQL 使用别名

php - 如何选择 MySQL 中最常见的值?

具有多个表的 MySQL 查询

mysql - GTID 复制失败错误 1062

mysql - 数据库格式化的实现

php - SailsJS 中的水线似乎没有捕捉到唯一约束

php - 从数字中找出月数、周数和天数

php - 从循环内的表中获取数组

php - AJAX 请求无效

php - Uncaught Error : Call to a member function generate_cart_id() on null