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>
这个剧本没有给我我对课堂上学生的期望。它也为那些得分低的人提供了不同的价值定位。 我需要帮助。
答案 0 :(得分:0)
此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
上添加索引以获得更好的效果。