简体   繁体   English

计数列值 Codeigniter

[英]Count column values Codeigniter

SQL: SQL:

table name "videos"表名“视频”

column name "views"列名“视图”

I wanna count all views and showing in User Page.我想计算所有视图并在用户页面中显示。

Example:例子:

Result SQL:结果 SQL:

-- views -- 3239 -- 浏览量 -- 3239

2918 2918

2345 2345

1928 1928年

32329 32329


total views: 42759总浏览量:42759

I've written a code which worked for me, I've explained the code wherever necessary.我已经编写了一个对我有用的代码,我已经在必要时解释了代码。 See if it helps you.看看对你有没有帮助。

$query = $this->db->select_sum('views')->get('videos')->result();  // gets the sum of data in views column.

$totalViews = $query[0]->views;  // the array returned contains the sum in key {0}

echo $totalViews;  // echo or return the output -- your logic

You can pull the data from the database then use the array_sum() function.您可以从数据库中提取数据,然后使用array_sum() function。 For example:例如:

$builder = $db->table('mytable');
$query = $builder->get()->getResultArray();

$totalViews = array_sum($query['views']);

echo $totalViews;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM