0

This is the query which is already ordered by points:

$data['users'] = $this->db->select( array( 'users_avatar', 'users_teams_id', 'users_id', 'users_name', 'users_win', 'users_lose', 'users_points', 'daily_points' ) )->order_by( 'daily_points', 'desc' )->limit( 26 )->get( 'users' )->result();

So, it working correctly in the displaying top users by points but however if top users have same points they are appearing really weird. I want to make sure at least top 3 users if same points to display them descending on the name but it seems it must be done in the foreach loop which i am not sure how to do it correctly. Below is the displaying code: (top 3 users have medal appearing based on 1st,2nd and 3rd thats why its like that with divs for th)

       <div class="dailyleft" id="teams_inner">
            <div id="teams_for_scroll">
            <?
                $i = 1;
        sort($i);
                foreach($users as $user)
                {
                    echo '
                <div class="row">'.(


                    (($i == '1')?'<div class="num_1"></div>':'').
                    (($i == '2')?'<div class="num_2"></div>':'').
                    (($i == '3')?'<div class="num_3"></div>':'').
                    (($i > '3')?'<div class="num">'.$i.'</div>':'')


                    ).'<img src="'.base_url().'public/uploads/t_'.$user->users_avatar.'" />
                    <div class="title" style="width:400px;">'.($user->users_teams_id?'<a class="team_link" href="'.site_url('teams/view/'.$user->users_teams_id).'">['.$user_teams[$user->users_teams_id].']</a>':'').' '.htmlspecialchars($user->users_name, ENT_QUOTES).'</div>
                    <div class="points" style="width:150px;"><span>Daily Points</span><font color="orange">'.number_format($user->daily_points, 0, ' ', ' ').'</font></div>
                    <div class="points" style="width:230px;"><span>All Time Win/ Lose</span>'.number_format($user->users_win, 0, ' ', ' ').' / '.number_format($user->users_lose, 0, ' ', ' ').'</div>
                </div>
                    ';
                    $i++;
                }
            ?>
            </div>
       </div>
MobEn
  • 65
  • 9

1 Answers1

0

You probably want to use usort on your initial $data['users'] array. Write a comparator that uses the value you care to sort on, and off you go.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • Thanks a lot for the answer but is it possible to provide an example how it should be structured and etc? Will be really greatly appreciated. Thanks again. – MobEn Sep 28 '14 at 21:20
  • the PHP manual has a *huge* comments section covering anything and everything you could possible ever want to do, which is why I'll only give you the link to the usort docs in the answer, and you get to scroll through it. There's a lot of very good, very useful information there, covering more than you need to do what you want to do. – Mike 'Pomax' Kamermans Sep 28 '14 at 21:26