簡體   English   中英

我想在考試成績中顯示第 1、2、3 位

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

我創建了一個考試結果表,我想顯示所有學生的位置,例如 1st、2nd、3rd,.... 就像他的百分比。

在此處輸入圖片說明

我想要這個結果。 在此處輸入圖片說明

這是我的代碼。

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

我正在嘗試按百分比獲得學生的排名位置。 例如,如果學生 1 的得分為 90.0%,總分為 100,學生 2 的得分為 80.5%,總分為 100。 學生 1 的排名將高於學生 2。我想知道我如何才能做到這一點?

您可以使用array_multisortarray_walk對記錄進行排名。 例如

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

輸出

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM