Ryahn
Ryahn

Reputation: 557

List all info within table into html table

I can't remember on how to do this. I am trying to take all the entries from a table within a database an list it into an html table. I am also trying to limit the number of entries pulled from the table to only 10 to 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>

Upvotes: 0

Views: 189

Answers (5)

Mukesh Chapagain
Mukesh Chapagain

Reputation: 26016

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

Upvotes: 0

FluffyJack
FluffyJack

Reputation: 1732

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>

Upvotes: 0

Kalaiyarasan
Kalaiyarasan

Reputation: 13454

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

just try this code

Upvotes: 1

AboQutiesh
AboQutiesh

Reputation: 1716

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

Upvotes: 0

gaurang171
gaurang171

Reputation: 9090

it should be like this

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

Upvotes: 0

Related Questions