在html表中列出表中的所有信息


List all info within table into html table

我不记得该怎么做了。我正试图将数据库中一个表中的所有条目列表到一个html表中。我还试图将从表中提取的条目数量限制在10到20个。

//Connection Info
include('data.php');
//Query To Pull Data
$sql = mysql_query("select name, death, model, humanity, hkills, bkills, kills, hs, late, ldrank, stime, survival, lastupdate from $stats order by kills limit 10");
//Data Pulled To Be Displayed
while ($row = mysql_fetch_array($sql)) {
    //Username
    $name = $row['name'];
    //Deaths
    $death = $row['death'];
    //Amount of Humanity
    $humanity = $row['humanity'];
    //Player Type
    $model = $row['model'];
    //Murders
    $murder = $row['hkills'];
    //Bandit Kills
    $bandit = $row['bkills'];
    //Zombie Kills
    $zombie = $row['kills'];
    //Head Shots
    $head = $row['hs'];
    //Unknown
    $late = $row['late'];
    //Unknown
    $ldrank = $row['ldrank'];
    //Unknown
    $stime = $row['stime'];
    //Time Survived
    $survival = $row['survival'];
    //Last Time Player Was On
    $update = $row['lastupdate'];
}

?>
<!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>
<table width="1000" border="1">
  <tr>
    <th scope="col">Username</th>
    <th scope="col">Type</th>
    <th scope="col">Friendliness</th>
    <th scope="col">Deaths</th>
    <th scope="col">Murders</th>
    <th scope="col">Bandit Kills</th>
    <th scope="col">Zombie Kills</th>
    <th scope="col">Head Shots</th>
    <th scope="col">Late</th>
    <th scope="col">STime</th>
    <th scope="col">Time Survived</th>
    <th scope="col">LDRank</th>
    <th scope="col">Last Played</th>
  </tr>
  <tr>
    <td><? echo "$name"; ?></td>
    <td><? echo "$model"; ?></td>
    <td><? echo "$humanity"; ?></td>
    <td><? echo "$death"; ?></td>
    <td><? echo "$murder"; ?></td>
    <td><? echo "$bandit"; ?></td>
    <td><? echo "$zombie"; ?></td>
    <td><? echo "$head"; ?></td>
    <td><? echo "$late"; ?></td>
    <td><? echo "$ldrank"; ?></td>
    <td><? echo "$stime"; ?></td>
    <td><? echo "$survival"; ?></td>
    <td><? echo "$update"; ?></td>
  </tr>
</table>
</body>
</html>
//Connection Info
include('data.php');
//Query To Pull Data
$sql = mysql_query("select name, death, model, humanity, hkills, bkills, kills, hs, late, ldrank, stime, survival, lastupdate from $stats order by kills limit 10");


?>
<!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>
<table width="1000" border="1">
  <tr>
    <th scope="col">Username</th>
    <th scope="col">Type</th>
    <th scope="col">Friendliness</th>
    <th scope="col">Deaths</th>
    <th scope="col">Murders</th>
    <th scope="col">Bandit Kills</th>
    <th scope="col">Zombie Kills</th>
    <th scope="col">Head Shots</th>
    <th scope="col">Late</th>
    <th scope="col">STime</th>
    <th scope="col">Time Survived</th>
    <th scope="col">LDRank</th>
    <th scope="col">Last Played</th>
  </tr>
<?php
while ($row = mysql_fetch_array($sql)) {
?>
  <tr>
    <td><? echo $row["$name"]; ?></td>
    <td><? echo $row["$model"]; ?></td>
    <td><? echo $row["$humanity"]; ?></td>
    <td><? echo $row["$death"]; ?></td>
    <td><? echo $row["$murder"]; ?></td>
    <td><? echo $row["$bandit"]; ?></td>
    <td><? echo $row["$zombie"]; ?></td>
    <td><? echo $row["$head"]; ?></td>
    <td><? echo $row["$late"]; ?></td>
    <td><? echo $row["$ldrank"]; ?></td>
    <td><? echo $row["$stime"]; ?></td>
    <td><? echo $row["$survival"]; ?></td>
    <td><? echo $row["$update"]; ?></td>
  </tr>
<?php } ?>
</table>
</body>
</html>

只需尝试此代码

它应该像这个

    <table width="1000" border="1">
      <tr>
        <th scope="col">Username</th>
        <th scope="col">Type</th>
        <th scope="col">Friendliness</th>
        <th scope="col">Deaths</th>
        <th scope="col">Murders</th>
        <th scope="col">Bandit Kills</th>
        <th scope="col">Zombie Kills</th>
        <th scope="col">Head Shots</th>
        <th scope="col">Late</th>
        <th scope="col">STime</th>
        <th scope="col">Time Survived</th>
        <th scope="col">LDRank</th>
        <th scope="col">Last Played</th>
      </tr>
    <?php
    while ($row = mysql_fetch_array($sql)) { ?>
    <tr>
        <td><?php echo $row['name']; ?></td>
        // Rest of other values
    <tr>
   <?php } //End while?>
    </table>
//Connection Info
include('data.php');
$dataFromTable = '';
//Query To Pull Data
$sql = mysql_query("select name, death, model, humanity, hkills, bkills, kills, hs, late, ldrank, stime, survival, lastupdate from $stats order by kills limit 0,20");
//Data Pulled To Be Displayed
while ($row = mysql_fetch_array($sql)) {
    //Username
    $name = $row['name'];
    //Deaths
    $death = $row['death'];
    //Amount of Humanity
    $humanity = $row['humanity'];
    //Player Type
    $model = $row['model'];
    //Murders
    $murder = $row['hkills'];
    //Bandit Kills
    $bandit = $row['bkills'];
    //Zombie Kills
    $zombie = $row['kills'];
    //Head Shots
    $head = $row['hs'];
    //Unknown
    $late = $row['late'];
    //Unknown
    $ldrank = $row['ldrank'];
    //Unknown
    $stime = $row['stime'];
    //Time Survived
    $survival = $row['survival'];
    //Last Time Player Was On
    $update = $row['lastupdate'];
   $dataFromTable .= "<tr><td>$name</td>
    <td>$model</td>
    <td>$humanity</td>
    <td>$death</td>
    <td>$murder</td>
    <td>$bandit</td>
    <td>$zombie</td>
    <td>$head</td>
    <td>$late</td>
    <td>$ldrank</td>
    <td>$stime</td>
    <td>$survival</td>
    <td>$update</td></tr>"
}

?>
<!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>
<table width="1000" border="1">
  <tr>
    <th scope="col">Username</th>
    <th scope="col">Type</th>
    <th scope="col">Friendliness</th>
    <th scope="col">Deaths</th>
    <th scope="col">Murders</th>
    <th scope="col">Bandit Kills</th>
    <th scope="col">Zombie Kills</th>
    <th scope="col">Head Shots</th>
    <th scope="col">Late</th>
    <th scope="col">STime</th>
    <th scope="col">Time Survived</th>
    <th scope="col">LDRank</th>
    <th scope="col">Last Played</th>
  </tr>
    <?php echo $dataFromTable;?>
</table>
</body>
</html>

如果您试图列出所有条目,那么您可以在while循环中为数据包装表行。请参阅下面的示例,该示例与您原来的示例有所不同。基本上,PHP只会在循环开始后处理?>,在循环仍在运行时在循环结束时处理<?php,然后继续正常执行。通过这种方式,您可以将表格行包装在while look中,因为表格行在循环中,它将与变量一起打印出来,循环中的每个项目都应该是10个项目,因为这就是您定义的限制。我希望我正确理解了这个问题,这有助于:)

//Connection Info
include('data.php');
//Query To Pull Data
$sql = mysql_query("select name, death, model, humanity, hkills, bkills, kills, hs, late, ldrank, stime, survival, lastupdate from $stats order by kills limit 10");
?>
<!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>
<table width="1000" border="1">
  <tr>
    <th scope="col">Username</th>
    <th scope="col">Type</th>
    <th scope="col">Friendliness</th>
    <th scope="col">Deaths</th>
    <th scope="col">Murders</th>
    <th scope="col">Bandit Kills</th>
    <th scope="col">Zombie Kills</th>
    <th scope="col">Head Shots</th>
    <th scope="col">Late</th>
    <th scope="col">STime</th>
    <th scope="col">Time Survived</th>
    <th scope="col">LDRank</th>
    <th scope="col">Last Played</th>
  </tr>
  <?php while ($row = mysql_fetch_array($sql)) { ?>
  <tr>
    <td><? echo $row['name']; ?></td>
    <td><? echo $row['model']; ?></td>
    <td><? echo $row['humanity']; ?></td>
    <td><? echo $row['death']; ?></td>
    <td><? echo $row['hkills']; ?></td>
    <td><? echo $row['bkills']; ?></td>
    <td><? echo $row['kills']; ?></td>
    <td><? echo $row['hs']; ?></td>
    <td><? echo $row['late']; ?></td>
    <td><? echo $row['ldrank']; ?></td>
    <td><? echo $row['stime']; ?></td>
    <td><? echo $row['survival']; ?></td>
    <td><? echo $row['lastupdate']; ?></td>
  </tr>
  <?php } ?>
</table>
</body>
</html>

应该是这样的:

<table width='80%' border=0>
    <tr bgcolor='#CCCCCC'>
        <th>Username</th>
        <th>Type</th>
        <th>Deaths</th>     
    </tr>
        <?php 
        while($row = mysql_fetch_array($sql)) {
        ?>
            <tr>
                <td><?php echo $row['name'] ?></td>
                <td><?php echo $row['model'] ?></td>
                <td><?php echo $row['death'] ?></td> 
            </tr>
        <?php 
        }
        ?>
</table>

你说你只需要10到20个条目。虽然不清楚,但如果你只想从第10行到第20行的条目,那么你可以通过以下方式使用限制:

SELECT * FROM `your_table` LIMIT 10, 10 

这将从第10个记录返回10条记录,即从第10条记录返回到第20条记录。

关于SQL限制:http://php.about.com/od/mysqlcommands/g/Limit_sql.htm