简体   繁体   中英

Order function doesn't do anything in PHP (CodeIgniter)

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 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);

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