简体   繁体   中英

Display top 10 results in codeigniter and display 1st, 2nd and 3rd top values

I am getting output page like this (Shown in below screenshot 参考下图 )

I want gold image to be display to top scorers and silver to second topper and bronze to others

I am using code like this

<div class="row">
<div class="col-lg-12">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-tasks"></i> ScoreBoard</h3>
        </div>
        <div class="panel-body">
           <div class="col-sm-12">
                <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered">

                    <tbody>
                        <?php
                            $total_exams1 = $this->db->select('title_id')   
                                    ->from('exam_title')
                                    ->count_all_results();

                                $b1 = $this->db->select('*, (sum(result.result_percent)) / '.$total_exams1.' as percent')   
                                                    ->group_by('users.user_id')                                     
                                                    ->from('result')
                                                    ->order_by("percent", "desc" )
                                                    ->join('users', 'users.user_id = result.user_id', 'left')
                                                    ->join('states', 'users.state = states.state_id', 'left')
                                                    ->join('user_zone', 'users.user_zone = user_zone.user_zone_id', 'left')
                                                    ->limit("10")
                                                    ->get()
                                                ->result(); 
                            $j = 1; 
                            foreach($b1 as $z) {                                            
                        ?>
                            <tr class="<?= ($i & 1) ? 'even' : 'odd'; ?>">                                    
                                <td style="width:5%;"><?php echo $j; ?></td> 
                                    <?php if($z->image == "") { ?>
                                <td class="hidden-x" style="width:35%;">    
                                    <img class="userImgTop10n" src="<?php echo base_url('user-avatar/avatar-placeholder.jpg') ?>" alt="Profile Picture" />
                                    <div class="image_righ">
                                        <b><?php echo $z->user_name; ?></b><br>
                                        <?php echo $z->state_name; ?><br>
                                        <?php echo $z->user_zone_name; ?>
                                    </div>
                                </td>                                   
                                    <?php } else { ?>
                                <td class="hidden-x" >
                                    <img class="userImgTop10n" src="<?php echo base_url("user-avatar/".$z->image); ?>" alt="Profile Picture" />
                                    <div class="image_righ">
                                        <b><?php echo $z->user_name; ?></b><br>
                                        <?php echo $z->state_name; ?><br>
                                        <?php echo $z->user_zone_name; ?>
                                    </div>
                                </td>                                   
                                    <?php } ?>          
                                <?php                   
                                    $exams_attended1 = $this->db->select('title_id')    
                                        ->where('user_id', $z->user_id)
                                        ->from('result')
                                        ->group_by('user_id')
                                        ->count_all_results();
                                ?>
                                <td class="hidden-xxs"><b><?php echo $exams_attended1; ?></b><br>Exams Attended</td>
                                <td class="hidden-xxs"><b><?php echo $total_exams1; ?></b><br>Total Exams</td>
                                <td class="hidden-x">
                                    <b><?php echo round($z->percent, 2); ?> %</b><br>Avg Result 
                                          <div class="badge_righ">
                                          <?php if($j == 1) { ?>                                              
                                          <span><img class="userBadge" src="<?php echo base_url('Badge_Gold.png') ?>" alt="Badge Gold" /></span>                                             
                                          <?php } 
                                          if($j == 2) { ?>                                           
                                          <span><img class="userBadge" src="<?php echo base_url('Badge_Silver.png') ?>" alt="Badge Gold" /></span>                                            
                                          <?php } if($j > 2) { ?>                                              
                                          <span><img class="userBadge" src="<?php echo base_url('Badge_Bronze.png') ?>" alt="Badge Gold" /></span>                                    
                                          <?php } ?>
                                          </div>
                                </td>                                   
                            </tr>
                            <?php 
                            $j++;
                                }
                            ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

in the above screenshot first 3 rows should be gold images (because of same marks).

You can create a temporary array containing unique reverse sorted average scores. Then, when giving medals, instead of checking for places (1st, 2nd, 3rd...), check if the score matches the 1st, 2nd or 3rd score from that temporary array.

For the screenshot you posted, the array would look like:

$scores = [92.5, 90, 87.5];

and the gold medals would be all the scores with the score of $scores[0] , the silver ones are $scores[1] and the bronzes would be $scores[2] (unless you want everyone else to get bronze).

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