尝试在网页中打印数据库,但无法获得表格的计数器

时间:2012-12-18 01:04:41

标签: php html mysql

我正在尝试创建一个页面,显示来自网页上的mysql数据库的rpg排名

我进入数据库并按级别组织数据,因此首先显示最高级别的人,依此类推,但数据库不包含排名表

我试图在Dreamweaver中手动执行此操作,但我不知道如何创建计数器并将其显示在表格中?

我设法得到以下内容: http://slayersgaming.com/rpgranks2.php

<?php
$dbh=mysql_connect("********", "**********", "*********") or die('Cannot connect to the database because: '. mysql_error());
mysql_select_db("C368969_thcrpgCSGO");

$rpgranks_sql = "SELECT * FROM `thc_rpg` ORDER BY `thc_rpg`.`level` DESC LIMIT 0, 30 ";

$rpgranks_query = mysql_query($rpgranks_sql) or die(mysql_error());

$rsrpgranks = mysql_fetch_assoc($rpgranks_query);


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>




<p>
<table width="501" height="58" border="0">
  <tr>
    <td width="240"><h2><strong>Name</strong></h2></td>
    <td width="70"><strong>Level</strong></td>
    <td width="70"><strong>XP</strong></td>
    <td width="70"><strong>Credits</strong></td>
    <td width="70"><strong>Rank</strong></td>
  </tr>
</table>
  <?php do{ ?>
<table width="501" height="58" border="0">
  <tr>
    <td width="240"><?php echo $rsrpgranks['name'];?></td>
    <td width="70"><?php echo $rsrpgranks['level'];?></td>
    <td width="70"><?php echo $rsrpgranks['xp'];?></td>
    <td width="70"><?php echo $rsrpgranks['credits'];?></td>
    <td width="70">&nbsp;</td>
  </tr>
</table>
  <?php } while ($rsrpgranks = mysql_fetch_assoc($rpgranks_query)) ?>


</p>
<p>&nbsp;       </p>
</body>
</html>

屏幕上限:http://oi47.tinypic.com/21exdfs.jpg

1 个答案:

答案 0 :(得分:0)

实际上这很容易做到。你只需要在你的do-while循环中使用一个计数器:

<?php
$counter = 1;
do{
?>
<table width="501" height="58" border="0">
  <tr>
    <td><?php echo $counter; ?></td>
    <td width="240"><?php echo $rsrpgranks['name'];?></td>
    <td width="70"><?php echo $rsrpgranks['level'];?></td>
    <td width="70"><?php echo $rsrpgranks['xp'];?></td>
    <td width="70"><?php echo $rsrpgranks['credits'];?></td>
    <td width="70">&nbsp;</td>
  </tr>
</table>
<?php
    $counter++;
} while ($rsrpgranks = mysql_fetch_assoc($rpgranks_query))
?>