我正在使用CodeIgniter作为框架。 现在我想订购一个列表,但它不会对DESC或ASC做出反应。
我的代码是:
function get_community($limit, $smallicon = false) { $this->db->select('user_to_designment.user_id, count(user_to_designment.user_id) as designment_joined, user_profiles.first_name, user_profiles.middle_name, user_profiles.last_name'); $this->db->from('user_to_designment'); $this->db->join('user_to_user_profile', 'user_to_designment.user_id = user_to_user_profile.user_id'); $this->db->join('user_profiles', 'user_to_user_profile.profile_id = user_profiles.profile_id'); $this->db->group_by('user_id'); $this->db->order_by('designment_joined', 'desc'); $this->db->limit($limit); $communitys = $this->db->get()->result_array(); foreach ($communitys as &$community) { if($smallicon){ $community['image'] = self::get_small_user_icon_by_id($community['user_id']); }else{ $community['image'] = self::get_big_user_icon_by_id($community['user_id']); } } return $communitys; }I'm using CodeIgniter as a framework. Now I want to order a list but it doesn't react on DESC or ASC.
My code is:
function get_community($limit, $smallicon = false) { $this->db->select('user_to_designment.user_id, count(user_to_designment.user_id) as designment_joined, user_profiles.first_name, user_profiles.middle_name, user_profiles.last_name'); $this->db->from('user_to_designment'); $this->db->join('user_to_user_profile', 'user_to_designment.user_id = user_to_user_profile.user_id'); $this->db->join('user_profiles', 'user_to_user_profile.profile_id = user_profiles.profile_id'); $this->db->group_by('user_id'); $this->db->order_by('designment_joined', 'desc'); $this->db->limit($limit); $communitys = $this->db->get()->result_array(); foreach ($communitys as &$community) { if($smallicon){ $community['image'] = self::get_small_user_icon_by_id($community['user_id']); }else{ $community['image'] = self::get_big_user_icon_by_id($community['user_id']); } } return $communitys; }最满意答案
designment_joined是一个count(user_to_designment.user_id) ,它将给出相同的值。 所以你看不出差异。 直接在PHPMyAdmin中尝试查询并查看结果。 还尝试在orderby子句中更改字段名称并检查。
例如:
替换$this->db->order_by('designment_joined', 'desc'); with $this->db->order_by('user_to_designment.user_id', 'desc'); 并检查。
更新的代码:
$this->db->select('user_to_designment.user_id, count(user_to_designment.user_id) as designment_joined, user_profiles.first_name, user_profiles.middle_name, user_profiles.last_name'); $this->db->from('user_to_designment'); $this->db->join('user_to_user_profile', 'user_to_designment.user_id = user_to_user_profile.user_id'); $this->db->join('user_profiles', 'user_to_user_profile.profile_id = user_profiles.profile_id'); $this->db->order_by('user_to_designment.user_id', 'desc'); $this->db->limit($limit);designment_joined is a count(user_to_designment.user_id) which will give the same value. So you can not see the difference. Try the query directly in PHPMyAdmin and see the result. Also try to change the field name in orderby clause and check.
eg :
Replace $this->db->order_by('designment_joined', 'desc'); with $this->db->order_by('user_to_designment.user_id', 'desc'); and check.
Updated Code:
$this->db->select('user_to_designment.user_id, count(user_to_designment.user_id) as designment_joined, user_profiles.first_name, user_profiles.middle_name, user_profiles.last_name'); $this->db->from('user_to_designment'); $this->db->join('user_to_user_profile', 'user_to_designment.user_id = user_to_user_profile.user_id'); $this->db->join('user_profiles', 'user_to_user_profile.profile_id = user_profiles.profile_id'); $this->db->order_by('user_to_designment.user_id', 'desc'); $this->db->limit($limit);更多推荐
发布评论