检测从php查询中检索到的ID何时更改

时间:2018-03-05 17:43:23

标签: php

我正在运行查询,通过获取活动类型的平均分数(测验,考试等)并将其乘以指定的成绩百分比(例如:80)来获得学生的平均分数。 * 0.03)。

我按结果activity_IDStudent ID对结果进行了分组。

要获得学生成绩,我需要添加每种活动类型的分数。通过简单地添加每个活动类型的所有结果。 (例如:年级= 10.40 + 30 + 12;)

现在我的问题是当学生ID被更改时,前一个学生的结果被添加到新学生的结果中。 (例如:新学生成绩=以前的成绩,加上他自己的成绩)

我想检测

中学生证的更改时间
while($row = mysqli_fetch_array($result)) {
    $studentID_link = $row['studentID']; 
    $atName = $row['AT_name']; 
    $GLOBALS['AR_Score'] = $row['AVG(AR.Score)']; 
    $AT_gradePercentage =$row['AT_gradePercentage']; 
    $AT_gradePercentage /=100; 
    $periodGrades = $AR_Score*$AT_gradePercentage; 
    $variableName = $variableName + $periodGrades; 
} 

1 个答案:

答案 0 :(得分:0)

使用

mysqli_fetch_assoc()

将初始学生ID存储在循环外的临时变量中后检查id。将每个新行的学生ID与先前存储的临时ID进行比较。还要在循环外声明一个数组并使用

array_push()

将每个总数推到数组上。使用以学生ID作为关键字的关联数组,如果您无序访问学生ID或使用mysql中的索引来解决此问题,请避免使用array_push。

老实说,如果您希望我们为您重写代码,您可能需要重新检查您的sql或向我们展示。

更新: 如果当前的mysqli_fetch_array正在获取一个关联数组,你可以保留它,但是如果你遇到问题,请参考我的上述建议。

$curr_student = NULL;
$array = [];
while($row = mysqli_fetch_array($result)) {

    $studentID_link = $row['studentID']; 
    if($curr_student === NULL || $curr_student !== $studentID_link){

    $curr_student = $studentID_link;
    if(!array_key_exists($curr_student, $array))   $array[$curr_student] = 0; 

    }

    $atName = $row['AT_name']; 
    $GLOBALS['AR_Score'] = $row['AVG(AR.Score)']; 
    $AT_gradePercentage =$row['AT_gradePercentage']; 
    $AT_gradePercentage /=100; 
    $periodGrades = $AR_Score*$AT_gradePercentage; 
    $array[$curr_student] = $array[$curr_student] + $periodGrades; 


}

我不确定您的全局数组以及您如何使用它,但这应该可以将您的学生添加到每个数组成员中,而不管随机访问学生ID。