2012-09-13 24 views
0

我不記得如何做到這一點。我正試圖從數據庫中的一個表中的所有條目列表到一個HTML表。我也試圖從表中拉條目的數量限制爲只有10到20列表中的所有信息到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"); 

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

回答

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

只是試試這個代碼

0

如果你想列出所有的條目,那麼你可以在while循環中包裝數據的錶行。請參閱下面的示例,從您的原始示例更改。基本上這是如何工作的,PHP只會在循環結束時處理循環開始後的?>和循環結束時的<?php,而循環仍在運行,之後它將繼續正常執行。通過這種方式,您可以將表格行包裝在while外觀中,並且由於表格行在循環中,因此將會爲循環中的每個項目打印出變量,這應該是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> 
0

應該是這樣的:

<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