你需要連接,抓住所有的東西,然後回聲到table。連接代碼是從w3schools。假設有一個表stats
一個名爲username
和score
領域:
<html>
<body>
<table style="width:100%">
<tr>
<th>Username</th>
<th>Score</th>
</tr>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT username, score FROM stats ORDER BY ABS(score) DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["username"] . "</td><td>" . $row["score"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</table>
</html>
您需要使用ORDER BY,在SQL命令的結果,由score
列。
編輯#1:我改變ORDER BY score
到ORDER BY ABS(score) DESC
因爲我注意到它沒有命令權(inspired by this answer)
SQL FIDDLE(只是查詢):http://sqlfiddle.com/#!9/78f41/9/0
爲了突出你的名字,只要將輸出循環修改爲此,只需將background-color: #ffff99;
添加到該行,如果它匹配$yourusername
。您需要弄清楚如何獲取當前用戶的名稱,並將它設置爲$yourusername
。下面是編輯循環:
while($row = $result->fetch_assoc()) {
if ($yourusername == $row["username"]) {
echo "<tr style='background-color: #ffff99;'><td>" . $row["username"] . "</td><td>" . $row["score"] . "</td></tr>";
} else {
echo "<tr><td>" . $row["username"] . "</td><td>" . $row["score"] . "</td></tr>";
}
}
它採用style,檢查出cssdesk:http://www.cssdesk.com/sq7Qq
相關https://stackoverflow.com/questions/14025959/how-can-i-make-a-basic-php-scoreboard –
感謝,但它不是我所需要的。我想在遊戲中發短信給玩家自己的位置。從MySql –
我知道,這只是相關的。我正在寫一個答案。 –