我如何在桌面上显示排名

时间:2010-06-10 10:06:34

标签: php

下面是我的代码,我想知道如何在桌面上显示排名。我有所有排名,但我似乎无法根据总数从最高到最低显示等级。

<?php
require_once 'DB.php';

$dsn = array(
    'phptype'  => 'mysql',
    'username' => 'root',
    'password' => '',
    'hostspec' => 'localhost',
    'database' => 'ext',
);

$options = array(
    'debug'       => 2,
    'portability' => DB_PORTABILITY_ALL,
);

$db =& DB::connect($dsn, $options);
if (PEAR::isError($db)) {
    die($db->getMessage());
}

$data =& $db->getCol('SELECT  Totals FROM class order by Totals desc');

if (PEAR::isError($data)) {
    die($data->getMessage());
}


$rank = 0;
$lastScore = PHP_INT_MAX;
foreach( $data as $name=>$score ) {
  if ( $lastScore !== $score ) {
    $lastScore = $score;
    $rank += 1;
  }
  //printf("%s %d (%d)\n", $name, $score, $rank);
  //printf($rank);
}
echo "<table border='1'>
<tr>
<th>Rank</th>
<th>Eng_Grammar</th>
<th>Eng_Composition</th>
<th>Totals</th>
</tr>";
$result = mysql_query("SELECT * FROM class order by Totals Desc");

while($row = mysql_fetch_array($result))
  {

  echo "<tr>";
  echo "<td>" . $rank . "</td>";
  echo "<td>" . $row['Eng_Grammar'] . "</td>";
  echo "<td>" . $row['Eng_Composition'] . "</td>";
  echo "<td>" . $row['Totals'] . "</td>";
  echo "</tr>";
  }
echo "</table>";


?> 

1 个答案:

答案 0 :(得分:1)

此处are some answers