How do i show the rank on the table
<p>Below is my code and i am wondering how to show the rank on the table.I have all the ranks but i cannot seem to get the rank to display from the highest to the lowest according to the totals.</p>

<pre><code>&lt;?php
require_once 'DB.php';

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

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

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

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

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


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

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

  echo "&lt;tr&gt;";
  echo "&lt;td&gt;" . $rank . "&lt;/td&gt;";
  echo "&lt;td&gt;" . $row['Eng_Grammar'] . "&lt;/td&gt;";
  echo "&lt;td&gt;" . $row['Eng_Composition'] . "&lt;/td&gt;";
  echo "&lt;td&gt;" . $row['Totals'] . "&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  }
echo "&lt;/table&gt;";


?&gt; 
</code></pre>
