简体   繁体   中英

I want to show position 1st, 2nd, 3rd in exam result

I Create a exam result sheet I want to show Positions all of student like 1st, 2nd, 3rd, .... like his percentage.

在此处输入图片说明

I want this result. 在此处输入图片说明

This is my code.

 <table> <thead> <tr> <th>Name</th> <?php $total_sub = 0; ?> <?php foreach ($subject as $sub): ?> <?php if ($sub['sub_status']==1): ?> <th colspan="2"> <center><?php echo $sub['sub_code']; ?></center></th> <?php $total_sub = $total_sub+1; endif; ?> <?php endforeach; ?> <th colspan="2"><center> Total </center></th> <th><center>Per% </center></th> <th><center>Position</center></th> <!-- onclick="sortTable(<?php echo $total_sub+2 ?>)" --> </tr> <tr> <th> </th> <?php foreach ($subject as $sub): ?> <?php if ($sub['sub_status']==1): ?> <th> <center> OM </center></th> <th> <center> TM </center> </th> <?php endif; ?> <?php endforeach; ?> <th> <center> OM </center> </th> <th> <center> TM </center> </th> <th><center></center></th> <th> <center> </center></th> </tr> </thead> <tbody> <?php foreach ($student as $std): ?> <?php if ($std['enrolment_status']==1): ?> <tr> <?php $total = 0; $obtain = 0; ?> <td> <?php echo $std['student_registration_name'] ?> </td> <?php foreach ($subject as $sub): ?> <?php if ($sub['sub_status']==1): ?> <?php $rt='N'; $rtt ='N'; $code = $std['en_id']."-".$sub['sub_id']; foreach ($result as $res) { $rest = $res['enrolment_en_id']."-".$res['subject_sub_id']; if ($code === $rest) { $rt = $res['er_obtain']; $rtt = $res['er_total']; $total = $total + $res['er_total']; if ($rt == '-1') { $obtain = $obtain + 0; }else if($rt == '-2'){ $obtain = $obtain + 0; }else { $obtain = $obtain + $res['er_obtain']; } } } ?> <td><center><?php if ($rt == '-1') { echo "A"; }else if($rt == '-2'){ echo "-"; }else { echo $rt; } ?> </center></td> <td><center><?php if ($rt == '-1') { echo "A"; }else if($rt == '-2'){ echo "-"; }else { echo "$rtt"; } ?> </center></td> <?php endif; ?> <?php endforeach; ?> <td><center><?php echo $obtain ?> </center></td> <td><center><?php echo $total ?> </center></td> <td> <center> <?php if ($total!=0) { $per = $obtain/$total*100; echo number_format($per, 1); echo " %"; }else { echo "0 %"; } ?> </center> </td> <td> <center> </center> </td> </tr> <?php endif; ?> <?php endforeach; ?> </tbody> </table>

I'm trying to get the ranking position of a student by percentage. For example if student 1 has 90.0% with a total of 100 points and student 2 has 80.5% with a total of 100 points. Student 1 will ranking position higher than student 2. I was wondering how would I be able to do this?

You can use array_multisort , array_walk to rank the records. For example

$records = [
 0 => ['percentage' => 95],
 1 => ['percentage' => 91],
 2 => ['percentage' => 98],
 3 => ['percentage' => 70]
];
array_multisort(array_column($records, 'percentage'),SORT_DESC,$records);
array_walk($records, function(&$v,$k){
  $v['rank'] = $k + 1;
});
echo '<pre>';
print_r($records);

Output

Array
(
[0] => Array
    (
        [percentage] => 98
        [rank] => 1
    )

[1] => Array
    (
        [percentage] => 95
        [rank] => 2
    )

[2] => Array
    (
        [percentage] => 91
        [rank] => 3
    )

[3] => Array
    (
        [percentage] => 70
        [rank] => 4
    )

)

看看这个,这是在 mysql 中完成所有工作的一种方法,因此您可以只回显数组 - 它还考虑了关系https://www.oreilly.com/library/view/mysql-cookbook/ 0596001452/ch13s10.html

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