我有一个庞大的数据库,其中包含有关测试代码的学生信息以及这些测试代码所获得的分数。我需要为每个测试代码对应的学生重新计算百分位数。我有一系列测试代码的代码,但它不能正常工作。
function recompute_percentiles()
{
if($_REQUEST[testcode]=="CAT B1" or $_REQUEST[testcode]=="CAT B2" or $_REQUEST[testcode]=="CAT B3" or $_REQUEST[testcode]=="CAT B4")
{
echo "<br />Got testcode: ".$_REQUEST[testcode];
$getsortedq=mysql_query("SELECT username, section1right as m from kmarks where testcode='.$_REQUEST[testcode].' order by section1right DESC");
if(!$getsortedq)
echo "Could not get the sorted query";
else
echo "got the sorted query quick";
$totalcount=mysql_num_rows($getsortedq);
while($r=mysql_fetch_array($getsortedq))
{
$u=$r[username];
$m=$r[m];
$array[$u]=$m;
}
$array2=$array;
//print_r($array2);
$updated=0;
foreach($array as $key=>$value)
{
$countsame=0;
foreach($array2 as $k=>$v)
{
if($v>=$value)
$countsame++;
else
break;
}
$countless = $totalcount - $countsame;
reset($array2);
$percentile=round($countless/$totalcount*100,2);
$updatep1q=mysql_query("UPDATE kmarks set percentile1=$percentile where username='.$key.' and testcode='.$_REQUEST[testcode].'");
if(!$updatep1q)
echo "<br />Could not update p1 for username: ".$key;
else
$updated++;
}
echo "<br />Updated ".$updated." records in kmarks db, out of ".$totalcount." records for testcode ".$_REQUEST[testcode];
}
}