欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

【加精】thinkphp万能统计函数

程序员文章站 2022-03-21 13:26:29
...
这个函数可以直接统计用户总数,文章总数,文章总点击等等
此函数只支持 thinkphp V5以下版本 玩V5版请绕过
/**
* ThinkPHP 万能统计 v1.1 (作者:小曾)
* @param $table 数据库表名
* @param $where 查询条件 支持字符串或数组
* @param $field 查看字段 传入字段将统计此字段的总值
* @return 返回数字 无结果返回0
*/
function get_count($table,$where="",$field=""){
$db = M($table);
if($field){
$count = $db->where($where)->sum($field);
}else{
$count = $db->where($where)->count();
}
return $count ? $count : 0;
}


//使用示例

//统计用户总数
get_count('user');

//统计vip用户数
get_count('user','vip=1'); 或 get_count('user',array('vip'=>1));

//统计文章总点击量
get_count('article','','click');

//统计未审核的文章数
get_count('article','audit=0'); 或 get_count('article',array('audit'=>0));


//也可以在模板里使用

本的已有会员: {:get_count('user')}


//交流QQ群324098841
//论坛 http://www.flash127.com