Jump to content

Recommended Posts

<?php
include_once('config.php');
$session_indexx=$_SESSION['index_number'];
$session_termm=$_SESSION['term'];
$session_classs=$_SESSION['class'];
$session_yearr = $_SESSION['year'];
 // This is the query to generate the individual student class Position from the grand total.
$sql = "SELECT rank
        FROM (
            SELECT ord.id
                , @seq := @seq+1 as seq
                , @rank := CASE WHEN ord.grand_total = @prev
                                THEN @rank
                                ELSE @seq
                                END as rank
                , @prev := ord.grand_total as grand_total
            FROM (
                SELECT id
                    , g.grand_total
                FROM student_exam_result g
                ORDER BY grand_total DESC
                LIMIT 18446744073709551615
            ) ord
            JOIN (SELECT @seq:=0, @rank:=0,@prev:=0) init
        ) ranked
        JOIN student_exam_result g ON g.id = ranked.id
        WHERE class = ?
            AND index_number = ?
            AND term = ?
            AND year = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param('ssss', $session_classs, $session_indexx, $session_termm, $session_yearr);
$stmt->execute();
$stmt->bind_result($position);
$stmt->fetch();


?>

Hello guys, please help with issues from the above code, the issue is when i have multipe data on the database table for the exam results the student positions becomes incorrect, the code is supposed to calculate student results based on their grand_total and accepts the index_number,term, class and term as html inputs which is used to seect the students from the same class. The image below shows my database table

table.PNG.b0c7ff995d3d829a38314ef9010d9e02.PNG

 

from the table above, EKE DANIEL in JSS1 A with grand total of 979 is supposed to come second in class but the code throws 5 as shown in the resut below

table2.thumb.PNG.4857c2c8474fe978107daccbfcf62008.PNG

Please how do i solve this

Link to comment
https://forums.phpfreaks.com/topic/316052-result-position-generation/
Share on other sites

To address this issue, you can try adding an additional WHERE clause to the SQL query to filter the results based on the input class, as follows:

$sql = "SELECT rank
        FROM (
            SELECT ord.id
                , seq := seq+1 as seq
                , rank := CASE WHEN ord.grand_total = prev
                                THEN rank
                                ELSE seq
                                END as rank
                , prev := ord.grand_total as grand_total
            FROM (
                SELECT id
                    , g.grand_total
                FROM student_exam_result g
                WHERE g.class = ?
                ORDER BY grand_total DESC
                LIMIT 18446744073709551615
            ) ord
            JOIN (SELECT seq:=0, rank:=0,prev:=0) init
        ) ranked
        JOIN student_exam_result g ON g.id = ranked.id
        WHERE class = ?
            AND index_number = ?
            AND term = ?
            AND year = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param('sssss', $session_classs, $session_classs, $session_indexx, $session_termm, $session_yearr);
$stmt->execute();
$stmt->bind_result($position);
$stmt->fetch();

In this updated query, the WHERE g.class = ? clause has been added to the subquery to ensure that only students from the input class are included in the calculation of their positions. Additionally, the bind_param statement has been updated to include an additional s parameter to match the added input parameter.

Note that this solution assumes that the class column in the student_exam_result table exactly matches the input session_classs value. If there are any discrepancies between the input value and the column values, you may need to adjust the query accordingly.

Edited by Strider64
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.