我想在检查结果中显示位置1、2、3

时间:2019-04-27 06:49:45

标签: php

我创建一个考试结果表,希望向其显示所有学生的排名,例如第一名,第二名,第三名...。

enter image description here

我想要这个结果。 enter image description here

这是我的代码。

<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高。我想知道如何才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以使用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
    )

)

答案 1 :(得分:0)

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