我正在开发一个社区,我想在用户配置文件中显示很多用户统计信息。Codeigniter显示用户的统计信息
最佳做法是什么?
我现在有这个,但我不认为它做的最好的办法,所以我会很高兴的,你可以给我解释一下我能做些什么来加载大量的统计资料在我的查询:
//Get the stats of a user in hes profile
function getState($username) {
//it has false so it dont try to escape the COUNT()
$this->db->select('username, firstname, lastname, freetext, imgPath, city, hood, online, level,
COUNT(forumThread.id) as totalThreads,
COUNT(forumComments.id) as totalComments
', FALSE);
$this->db->join('forumThread', 'forumThread.creater = users.username');
$this->db->join('forumComments');
$this->db->group_by('users.id');
$this->db->where('username', $username);
$statQuery = $this->db->get('users');
return $statQuery->row();
}
您不必将select中的第二个参数设置为false,因为Codeigniter不会转义函数。实际上,源代码首先使用逗号作为分隔符来分割字符串。然后它会遍历这个数组,并且如果它们不在那里,就会向每个项目添加反引号,因为每个项目都被假定为一个字段,但是如果在任何地方找到了括号,它就会保持项目原样。 –