PHP代码根据他们的分数(1st,2nd,3rd,3rd,5th)给出位置


php code to give the position according to their scores (1st, 2nd, 3rd, 3rd, 5th)

CREATE TABLE IF NOT EXISTS `jss1_prelim` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`term` varchar(30) NOT NULL,
`idnumber` varchar(30) NOT NULL,
`subject` varchar(30) NOT NULL,
`score` varchar(30) NOT NULL,
`teacher` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
)   ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `jss1_prelim` (`id`, `term`, `idnumber`, `subject`, `score`, `teacher`)
VALUES
(1, 'Prelim', 'SBI-JS202', 'Computer', '82', 'TID273'),
(2, 'Prelim', 'SBI-JS645', 'Computer', '63', 'TID273'),
(3, 'Prelim', 'SBI-JS300', 'Computer', '65', 'TID273'),
(4, 'Prelim', 'SBI-JS202', 'Igbo', '70', 'TID015'),
(5, 'Prelim', 'SBI-JS645', 'Igbo', '65', 'TID015');

我需要有关此脚本的帮助,此脚本适用于对 sql 数据库条目进行排序。但是我需要 php 代码根据他们的分数(第 1、2、3、3、5)给出位置。

<table border = '2'>
<tr>
<td>Student ID</td>
<td>Score</td>
<td>Position</td>
</tr>
<?php
$con = mysql_connect("localhost", "root", "");
if(!$con)
{
    echo "Could not establish connection to the database " . mysql_error();
}
$mydb = mysql_select_db("sbicenugu", $con);
if (!$mydb)
{
    echo "Error selecting database " . mysql_error();
}
$mystatement = mysql_query("SELECT * FROM jss1_prelim ORDER BY score DESC");
$dbcount = mysql_num_rows($mystatement);
$selection = array(0 => "");
$nameselection = array(0 => "");
$i = 0; 
    while ($row = mysql_fetch_array($mystatement))
    {
    $selection[$i] = $row['score'];
    $nameselection[$i] = $row['idnumber'];
    $i++; 
    }
arsort($selection);
ksort($nameselection);
for ($x = 0; $x < $dbcount; $x++)
{
echo "<tr> <td>$nameselection[$x]</td> 
<td>$selection[$x]</td>
<td>"; $grades = array($selection[$x]);
$occurrences = array_count_values($grades);
$grades = array_unique($grades);
foreach($grades as $grade) 
{
if($grade == end($grades))$i += $occurrences[$grade]-1;
echo str_repeat('-'.($i + 1).'<br>',$occurrences[$grade]);
$i+= $occurrences[$grade];
}
echo"</td> 
</tr>"; 
}
?>
</table>

这个脚本没有给我对学生在课堂上的位置的期望。 此外,它为那些分数相同的人提供了不同的价值位置。我需要帮助。

此SQL代码将为您提供"rank"列的排名,并且可以很好地处理部分结果,因此即使显示多个页面的第二页,您也始终具有正确的排名:

SELECT (SELECT COUNT(*) + 1
    FROM jss1_prelim ref
    WHERE ref.score > main.score) as rank,
    id, term, idnumber, subject, score, teacher
FROM jss1_prelim main
ORDER BY score DESC

确保在score上放置索引以获得更好的性能。