简体   繁体   中英

Count column values Codeigniter

SQL:

table name "videos"

column name "views"

I wanna count all views and showing in User Page.

Example:

Result SQL:

-- views -- 3239

2918

2345

1928

32329


total views: 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. For example:

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

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

echo $totalViews;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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