任何人都可以帮助我在PHP和MySQL的这个排名?

时间:2013-07-19 09:29:41

标签: php mysql

我有如下的mysql表:

CREATE TABLE IF NOT EXISTS `cxexam` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`regd` int(11) NOT NULL,
`Name_of_Student` varchar(100) COLLATE latin1_general_ci NOT NULL,
`Class` varchar(50) COLLATE latin1_general_ci NOT NULL,
`Roll_no` int(11) NOT NULL,
`Section` varchar(50) COLLATE latin1_general_ci NOT NULL,
`Name_of_exam` varchar(100) COLLATE latin1_general_ci NOT NULL,
`Test_date` date NOT NULL,
`Subject` varchar(50) COLLATE latin1_general_ci NOT NULL,
`Full_mark` int(11) NOT NULL,
`Mark_score` int(11) NOT NULL,
`Year` year(4) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=7 ;

-

- 转储表cxexam

的数据
INSERT INTO `cxexam` (`id`, `regd`, `Name_of_Student`, `Class`, `Roll_no`, `Section`, `Name_of_exam`, `Test_date`, `Subject`, `Full_mark`, `Mark_score`, `Year`) VALUES

(6, 20, 'Ramdina', 'X', 9, 'A', 'Second Term Unit Test', '2013-07-19', 'English', 20, 18, 2013),
(3, 2, 'Zonundanga', 'X', 5, 'A', 'Second Term Unit Test', '2013-07-19', 'English',   20, 12, 2013),
(4, 40, 'Lalnunkimi', 'X', 10, 'A', 'Second Term Unit Test', '2013-07-19', 'English', 20, 18, 2013);

mysql查询将产生:

regd   totalscore    rank
20   18   1
2   18   1
40  12   2

我想使用php输出其regd='2'的排名。我使用以下PHP代码和mysql代码,但我需要刷新我的页面以使代码工作。查询在phpmyadmin中运行正常,但大多数情况下,即使我更改1,我也只获得regd排名。

mysql_select_db($database_dbconnect, $dbconnect);
$query_myrank = "SELECT Distinct regd, Test_date, Year, Name_of_Student, TOTALSCORE,  Rank FROM 
(SELECT *, IF(@marks = (@marks := TOTALSCORE), @auto, @auto := @auto + 1) AS
Rank FROM (SELECT Name_of_Student, regd, Test_date, Year, SUM(Mark_score) AS TOTALSCORE 
FROM cxexam, (SELECT @auto := 0, @marks := 0) AS init GROUP BY regd, Year ORDER BY TOTALSCORE DESC) t) AS result HAVING regd='2' and Year='2013' and Test_date between '2013-07-01' and '2013-07-30'";
$myrank = mysql_query($query_myrank, $dbconnect) or die(mysql_error());
$row_myrank = mysql_fetch_assoc($myrank);
$Rank= $row_myrank['Rank'];

我使用<?php echo $row_myrank['Rank'];?>回应它。如果有人能使用php为我提供替代解决方案,我会非常高兴,因为我正试图在我的学校标记管理系统中应用此排名。

这是我的新代码:

<?php 
mysql_select_db($database_dbconnect, $dbconnect);
$query_myrank = "SELECT Distinct regd, Test_date, Year, Name_of_Student, TOTALSCORE, Rank 
FROM (SELECT *, IF(@marks = (@marks := TOTALSCORE), @auto, @auto := @auto + 1) AS Rank 
FROM (SELECT Name_of_Student, regd, Test_date, Year, SUM(Mark_score) AS TOTALSCORE 
FROM cxexam, (SELECT @auto := 0, @marks := 0) AS init GROUP BY regd, Year ORDER BY TOTALSCORE DESC) t) AS result HAVING Year='$yr' and Test_date between '$fdate' and '$tdate'";
$myrank = mysql_query($query_myrank, $dbconnect) or die(mysql_error());

$i = 0;
$j = 1;
$data = array();
while($row_myrank = mysql_fetch_assoc($myrank))
{
$data[$i] = $row_myrank;
if(isset($data[$i - 1]) && $data[$i - 1]['TOTALSCORE'] == $data[$i]['TOTALSCORE'])
{
   $data[$i]['Rank'] = $j;
}else{
   $data[$i]['Rank'] = ++$j;
}
   $i++;
}
foreach($data as $key => $value)
{
if($value['regd'] == $regd)
{
echo $value['Rank'];
}
}
?>

1 个答案:

答案 0 :(得分:1)

当你进行循环时,添加一个这样的自动增量。

    $i = 0;
    $j = 0;

$data = array();

    while($row_myrank = mysql_fetch_assoc($myrank))
    {
      $data[$i] = $row_myrank;
      if(isset($data[$i - 1]) && $data[$i - 1]['Mark_Score'] == $data[$i]['Mark_Score'])
      {
       $data[$i]['rank'] = $j;
      }else{
       $data[$i]['rank'] = ++$j;
      }
      $i++;
    }

如果不说“不要使用mysql_ *函数而是PDO”,这将是一个糟糕的答案。请看这个链接。 http://www.php.net/manual/fr/class.pdo.php

编辑:如何输出数据

foreach($data as $key => $value)
{
 if($value['regd'] == 2)
 {
   echo $value['field'];
 }
}

只需按查询中的字段更改字段即可。