如何在php中评分

我正在尝试根据获得的最高平均分数对一些分数进行评分。 这是我的脚本


$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 键来分配成绩位置,但无法正常工作。这是我的结果

http://img3.mukewang.com/62ac465b0001e0a201290264.jpg

这是我需要实现的目标。


    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


噜噜哒
浏览 107回答 2
2回答

慕婉清6462132

如果已经设置了成绩,则不应更新您的记录。如果您打印查询而不是执行它,您将看到您设置了第 1 级,然后您用第 2 级覆盖它,然后用第 3 级覆盖它。

慕丝7291255

我认为你想要的代码是这样的:<?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) {&nbsp; &nbsp; [ $student_id, $score ] = explode('=', $record);&nbsp; &nbsp; $score_map[$student_id] = $score;}// Ensure values are sorted numerically descendinguasort($score_map, function ($a, $b) {&nbsp; &nbsp; if ($a > $b) {&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; if ($a == $b) {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }&nbsp; &nbsp; 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) {&nbsp; &nbsp; // If the score hasn't changed from it's previous value&nbsp; &nbsp; // we increment a counter instead of the rank&nbsp; &nbsp; if ($score == $previous_score) {&nbsp; &nbsp; &nbsp; &nbsp; $incrementer++;&nbsp; &nbsp; // Once it's changed, we update the rank based on the incrementer&nbsp; &nbsp; // and then reset the incrementer&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $rank += $incrementer;&nbsp; &nbsp; &nbsp; &nbsp; $incrementer = 1;&nbsp; &nbsp; }&nbsp; &nbsp; $previous_score = $score;&nbsp; &nbsp; // Updating database is left as an exercise to the reader}
打开App,查看更多内容
随时随地看视频慕课网APP