我正在尝试根据获得的最高平均水平对一些分数进行评分。 这是我的脚本
$scores_AND_ID = 'M2377O=100,M2727B=100,M5821K=100,M7492F=97.75,M7973O=96,M3487I=94,M7969O=93.13,M1452V=92.5,M4653O=92.38,M4158J=92.25,M2881A=89.38,M6112S=28.63,';
$out_score = chop($scores_AND_ID, ',');
$rr2 = explode(",", $out_score);
$array_un = array_unique($rr2);
foreach ($array_un as $key => $value) {
if ($value == "") {
continue;
}
$postion = positionNumbers($key);//1st,2nd,3rd function
$sec = explode("=", $value);
rsort($sec);
$stdntID = $sec[0]; //Student number
$stdntAV = $sec[1]; //Student Average
mysql_query("UPDATE score_table SET grade='$postion' WHERE avg='$stdntAV' ");
}
我正在使用foreach键来分配成绩位置,但工作不正常。 这是我的结果
这是我需要实现的。
1. 100---1st
2. 100---1st
3. 100---1st
4. 98---4th
5. 89.5--5th
6. 89---6th
7. 89---6th
8. 80---8th
谢谢你们
答案 0 :(得分:0)
如果已设置成绩,则不应更新记录。如果您打印查询而不是执行查询,则会看到您将等级设置为1st,然后将其覆盖为2nd,然后使用3rd。
答案 1 :(得分:0)
我认为您想要的代码是这样:
<?php
$scores_AND_ID = 'M7492F=97.75,M7973O=96,M3487I=94,M2377O=100,M2727B=100,M5821K=100,M7969O=93.13,M1452V=92.5,M4653O=92.38,M4158J=92.25,M2881A=89.38,M6112S=28.63,';
// Gets all of the entries from the source string
$scores_array = array_unique(array_filter(explode(',', $scores_AND_ID)));
// Sets up an associative array of values (student id => score)
$score_map = [];
foreach ($scores_array as $record) {
[ $student_id, $score ] = explode('=', $record);
$score_map[$student_id] = $score;
}
// Ensure values are sorted numerically descending
uasort($score_map, function ($a, $b) {
if ($a > $b) {
return -1;
}
if ($a == $b) {
return 0;
}
return 1;
});
// Gets the maximum value from the scores
$previous_score = max(array_values($score_map));
$rank = 1;
$incrementer = 0;
foreach ($score_map as $student => $score) {
// If the score hasn't changed from it's previous value
// we increment a counter instead of the rank
if ($score == $previous_score) {
$incrementer++;
// Once it's changed, we update the rank based on the incrementer
// and then reset the incrementer
} else {
$rank += $incrementer;
$incrementer = 1;
}
$previous_score = $score;
// Updating database is left as an exercise to the reader
}