php 根据所有结果的总分之和为班级中的每个学生分配班级位置

yx2lnoni  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(147)

请我试图分配类的位置,例如)第一,第二,第三等,在每个类根据谁有最高分时,总分数相加将不胜感激任何帮助The image added is my database

<div> 
 <?php
$classid;
$studentid;
$term;

$query8= $dbh->prepare("SELECT SUM(Aggregate) AS Aggregate , StudentId FROM tblresult WHERE ClassId=? and Term=? GROUP BY StudentId ORDER BY SUM(Aggregate) DESC");
 $query8->execute(array($classid,$term));

//$results8 = $query8-> fetchAll(PDO::FETCH_ASSOC);

 $results8 = $query8-> fetchAll(PDO::FETCH_ASSOC);
$i=0;
$u=0;
echo $studentid."<br>";
foreach($results8 as $result8){
    $i++;
     
    echo $Sid = $i.'-'.$result8['Aggregate'].'-----'.$Students_id=$result8['StudentId']."<br>";  
 //$position = array_search($studentid, $Students_id);
   
}

//$position = array_search($studentid, $Students_id);
//Position: echo $position;
?>
</div>
py49o6xq

py49o6xq1#

下面我是怎么做的,但是你必须把class_id和student_id作为列表来给予你一个有意义的结果。

try
    {
        $dbh = new PDO('mysql:host=localhost;dbname=db', 'user', 'pwd');
    }
    catch(Exception $e)
    {
        die('Erreur : '.$e->getMessage());
    }
    
    
    // $classid;
    // $studentid;
    $classid='18';
    $studentid='19';
    $term = 'First term';
    
    
    $query8 = $dbh->prepare("SELECT StudentId, SUM(Aggregate) FROM tblresult  WHERE ClassId=? and Term=? ORDER BY SUM(Aggregate) DESC");
    $query8->execute(array($classid,$term));
    
    $results8 = $query8-> fetchAll(PDO::FETCH_ASSOC);
    
    $position = 1;
    
    foreach($results8 as $result8){
        $studentName = $result8['StudentId'];
        $aggregateScore = $result8['SUM(Aggregate)'];
        echo $position . ": " . $studentName . " - " . $aggregateScore . "<br>";
        $position++;
    }

我模拟你的db表,结构如下:

相关问题