0

我不记得如何做到这一点。我正在尝试从数据库中的表中获取所有条目,并将其列表到 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>
4

5 回答 5

1
//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>

试试这段代码

于 2012-09-13T06:53:30.480 回答
0

应该是这样的

    <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>
于 2012-09-13T06:50:08.313 回答
0
//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>
于 2012-09-13T06:51:10.497 回答
0

If you're trying to list all the entries, then you can just wrap the table rows for the data in the while loop. See the example below altered from your original one. Basically how this works is PHP will only process the ?> after the start of the loop and the <?php at the end of the loop while the loop is still running, after which it will continue with normal execution. This way you can wrap the table row in the while look and because the table row is in the loop it will be printed out, with the variables, for each item in the loop, which should be 10 items as that's what you've defined the limit as. I hope I understood the question correctly and this helps :)

//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>
于 2012-09-13T06:56:22.040 回答
0

It should be something like this:

<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>

You said you need only 10 to 20 entries. Not clear though but if you want entries from 10th to 20th row only then you can use limit in the following way:

SELECT * FROM `your_table` LIMIT 10, 10 

This will return 10 records from 10th record, i.e. from 10th record to 20th record.

About SQL Limit: http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

于 2012-09-13T06:56:42.350 回答