How to display all students with the highest scores in each subject - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, September 1, 2022

How to display all students with the highest scores in each subject

I have a table where I display the students with the highest scores in individual subjects in a class.

However, I noticed that if two or more students have the same highest score in a subject, only one gets displayed.

This is my query:

<?php               
  $subList = $this->db->query("SELECT sub.id sub_id,sub.name subject_name, sub.code subject_code
    FROM 
    ".$table." mts 
    INNER JOIN subjects sub ON(sub.id=mts.subject_id)
    WHERE mts.class_id=".$class_id." AND `session_id` = '".$sch_setting->session_id."' AND mts.subject_id IS NOT NULL GROUP BY mts.subject_id ORDER BY sub.name");
  
    foreach($subList->result_array() as $subName){

        $subjectMaxQry = $this->db->query("SELECT a.student_id, students.firstname, students.lastname, a.subject_id, subjects.name, a.".$totField."
FROM ".$table." a
INNER JOIN (
    SELECT class_id, subject_id, MAX(".$totField.") highest
    FROM ".$table."
    GROUP BY class_id, subject_id
) b 
ON a.".$totField." = b.highest 
AND a.class_id = b.class_id 
AND a.subject_id = b.subject_id
INNER JOIN students ON students.id=a.student_id
INNER JOIN subjects ON subjects.id=a.subject_id
WHERE a.class_id = '".$class_id."'
AND a.session_id = '".$sch_setting->session_id."'
AND a.`subject_id` = '".$subName['sub_id']."'");
$highestMark = $subjectMaxQry->row_array();
 //echo '<pre>';print_r($highestMark);die;
                    ?>



<tr style="border: 1px solid black;">
  <td style="border: 1px solid black;font-size:12px;width:30px;text-align:left;"><?php echo $highestMark['firstname'].'&nbsp;'.$highestMark['lastname']; ?> </td>
  <td style="border: 1px solid black;font-size:12px;width:30px;text-align:left;"><?php echo $highestMark['name']; ?> </td>
  <td style="border: 1px solid black;font-size:12px;width:30px;text-align:center;"> <?php 
if($terms=='f'){
       echo $highestMark['f_tot_score']; 
      }elseif($terms=='t'){
       echo $highestMark['h_tot_score'];  
      }elseif($terms=='s'){
       echo $highestMark['tot_score'];
      }
      ?> </td>
  <td style="border: 1px solid black;font-size:12px;width:30px;text-align:center;"> </td>


</tr>

<?php } ?>
</tbody>
</table>
<div class="col-lg-12 text-center"><br><br>
  <input id="printbutton" class="btn btn-primary" value="Print" href="javascript:void(0);" onclick="printArea('printableArea')" />
</div>
</div>
</div>
</div>
</div>
<?php } ?>

This is what gets displayed. See image below:

maxscores table

As you can see, there are some empty rows. It does not display the other students who have the same highest score in a subject.

For example, Gil and Mary both got the highest score (98) in Maths. It only displays one name and leaves out the other.

How do I get all results to show?



source https://stackoverflow.com/questions/73560352/how-to-display-all-students-with-the-highest-scores-in-each-subject

No comments:

Post a Comment