Jump to content

Issue generating student class position based on the grand exam score


Royal

Recommended Posts

Hello guys, i need help to generate students class position in a class broadsheet based on their individual exam totals, the results are being populated from the database and everything works fine but i do not know how to generate the position on the fly from the grand totals generated from the database and store in a $total variable for each student. below is my code

<?php
include_once('../controller/config.php');
if (isset($_POST['view'])) {
	$term1 = $_POST['term'];
	$year1 = $_POST['year'];
	$class1 = $_POST['class'];
	
$sql1 = "SELECT * FROM student_exam_result where year = '$year1' and class = '$class1' and term = '$term1'";
	  	  
$result1=mysqli_query($conn,$sql1);
$row1=mysqli_fetch_assoc($result1);

$subject1 = $row1['subject1'];
$subject2 = $row1['subject2'];
$subject3 = $row1['subject3'];
$subject4 = $row1['subject4']; 
}
?>

<h3 style="text-align:justify !important; font-weight:bolder" class="box-title"><?php echo "$term1 ";?>RESULT BROADSHEET FOR CLASS <?php echo "$class1 ";?><?php echo"$year1";?>  </h3>
        </div><!-- /.box-header -->
        <div class="box-body table-responsive">
        <table style="width:100%" id="example1" class="table table-bordered table-striped">
                  <thead>
<th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-1">ID</th>	
<th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3">Student Name</th><th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3">Index Number</th>
<th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3"><?php echo $subject1; ?></th>
<th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3"><?php echo $subject2; ?></th>
<th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3"><?php echo $subject3; ?></th>
<th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3"><?php echo $subject4; ?></th>
<th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3">Grand Total</th>
 <th  style="background-color: gray;color: white; text-transform: uppercase;"class="col-md-8">Class Position</th> 
                </thead>
                <tbody>	
<?php
include_once('../controller/config.php');
if (isset($_POST['view'])) {
	$term = $_POST['term'];
	$year = $_POST['year'];
	$class = $_POST['class'];

	
$sql = "SELECT * FROM student_exam_result where year = '$year' and class = '$class' and term = '$term' ORDER BY id DESC";
	  	  
$result=mysqli_query($conn,$sql);
$count=0;
if(mysqli_num_rows($result) > 0){
	while($row=mysqli_fetch_assoc($result)){
		$count++;


		$subject1_total = $row['subject1_test1_score'] + $row['subject1_test2_score'] + $row['subject1_exam_score'];
		
        $subject2_total = $row['subject2_test1_score'] + $row['subject2_test2_score'] + $row['subject2_exam_score'];
        
        $subject3_total = $row['subject3_test1_score'] + $row['subject3_test2_score'] + $row['subject3_exam_score'];
        
        $subject4_total = $row['subject4_test1_score'] + $row['subject4_test2_score'] + $row['subject4_exam_score'];

        $total = $row['grand_total'];
?>   			

                	<tr>
                    	<td><?php echo $count; ?></td>
                        <td> <?php echo $row['name']; ?> </td>
                        <td> <?php echo $row['index_number']; ?> </td>
                        <td> <?php echo $subject1_total; ?></td>
                        <td> <?php echo $subject2_total;  ?></td>
                        <td> <?php echo $subject3_total; ?></td>
                        <td> <?php echo $subject4_total;  ?></td>
                        <td> <?php echo $total; ?></td>
                        <td><?php echo $position?> </td>
                        </tr>
<?php } } } ?>
                </tbody>
         	</table>	
     	</div><!-- /.box-body -->

Below is the php code i used to calculate the position

        // Sample array of students and their exam totals
$studentScores = array($total);

// Sort the array in descending order of scores
arsort($studentScores);

// Get the positions of students
$positions = array();
$position = 1;
$prevScore = null;

foreach ($studentScores as $student => $score) {
    if ($prevScore !== null && $score < $prevScore) {
        $position++;
    }

    $positions[$student] = $position;
    $prevScore = $score;
}

// Print the positions of students
foreach ($positions as $student => $position) {
    echo $position . PHP_EOL;
}

But it gives me 1 as every students position. Please how do i solve this

Link to comment
Share on other sites

Here's one way

    $student_scores = [ 
                        [ 'name' => 'Student A', 'score' => 68, 'pos' => '' ],
                        [ 'name' => 'Student B', 'score' => 52, 'pos' => '' ],
                        [ 'name' => 'Student C', 'score' => 73, 'pos' => '' ],
                        [ 'name' => 'Student D', 'score' => 80, 'pos' => '' ],
                        [ 'name' => 'Student E', 'score' => 56, 'pos' => '' ],
                        [ 'name' => 'Student F', 'score' => 77, 'pos' => '' ],
                        [ 'name' => 'Student G', 'score' => 73, 'pos' => '' ],
                        [ 'name' => 'Student H', 'score' => 49, 'pos' => '' ],
                        [ 'name' => 'Student I', 'score' => 88, 'pos' => '' ],
                        [ 'name' => 'Student J', 'score' => 90, 'pos' => '' ]
                      ];
    $scores = array_column($student_scores, 'score');
    rsort($scores);
    foreach ($student_scores as &$s) {
        $s['pos'] = array_search($s['score'], $scores) + 1;
    }
    // sort scores by position
    uasort($student_scores, fn($a, $b) => $a['pos']<=> $b['pos']);
    // show results                                
    echo '<pre>' . print_r($student_scores, 1) . '</pre>';

giving

Array
(
    [9] => Array
        (
            [name] => Student J
            [score] => 90
            [pos] => 1
        )

    [8] => Array
        (
            [name] => Student I
            [score] => 88
            [pos] => 2
        )

    [3] => Array
        (
            [name] => Student D
            [score] => 80
            [pos] => 3
        )

    [5] => Array
        (
            [name] => Student F
            [score] => 77
            [pos] => 4
        )

    [2] => Array
        (
            [name] => Student C
            [score] => 73
            [pos] => 5
        )

    [6] => Array
        (
            [name] => Student G
            [score] => 73
            [pos] => 5
        )

    [0] => Array
        (
            [name] => Student A
            [score] => 68
            [pos] => 7
        )

    [4] => Array
        (
            [name] => Student E
            [score] => 56
            [pos] => 8
        )

    [1] => Array
        (
            [name] => Student B
            [score] => 52
            [pos] => 9
        )

    [7] => Array
        (
            [name] => Student H
            [score] => 49
            [pos] => 10
        )

)

 

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.