1

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

enter image description here

I want this result. enter image description here

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?

ahmed
  • 7
  • 3

2 Answers2

1

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
    )

)
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

have a look at this, it is a way to do all the work in mysql so you can just echo out the array - also it takes into account ties https://www.oreilly.com/library/view/mysql-cookbook/0596001452/ch13s10.html

imposterSyndrome
  • 896
  • 1
  • 7
  • 18