0

我正在制作一个足球网站。我有以下表结构:

season1 (name of the table)
id | player1 | player2 | score

我想把分数放到一个 html 表中。此表仅适用于名为nickname1 的玩家与其他所有玩家。我有这个:

$query = mysql_query("SELECT * FROM season1 WHERE player1='nickname1'") or die(mysql_error());  

while($row = mysql_fetch_array( $query )) {
    echo "<tr><td>"; 
    echo "";
    echo "</td><td>";   
    echo $row['score2'];
    echo "</td><td>"; 
    echo $row['score3']; 
    echo "</td><td>"; 
    echo $row['score4'];  
    echo "</td><td>";   
    echo $row['score5'];  
    echo "</td></tr>"; 
} 
echo "</table> <br>";
  • 而不是 $row['score2'] 我需要昵称1与昵称2之间的分数
  • 而不是 $row['score3'] 我需要昵称1与昵称3之间的分数
  • 而不是 $row['score4'] 我需要昵称1与昵称4之间的分数
  • 而不是 $row['score5'] 我需要昵称1与昵称5之间的分数

我试过SELECT * FROM season1 WHERE player1='nickname1' AND player2='nickname2'了,但它只会在表格中显示 player1 vs player2。我想在同一张桌子上显示所有 4 名玩家 vs player1。

4

2 回答 2

0

You need to focus your loop only on making one row:

echo "<table><tr>";
while($row = mysql_fetch_array( $query )) {
    echo "<td>{$row['score']}</td>";
}
echo "</tr></table>";

This will ONLY output the scores of the players. You can modify it to have nickname headers and so forth.

Take note that the $row array resembles your table structure. $row['score'] will give you the scores of the players where as since score1 is not a column in the table, it does not exist in $row.

Possible design:

echo "<table><tr><th>Player 1</th><th>Player 2</th><th>Score</th></tr><tr>";
while($row = mysql_fetch_array( $query )) {
    echo "<td>{$row['player1']}</td>";
    echo "<td>{$row['player2']}</td>";
    echo "<td>{$row['score']}</td>";
}
echo "</tr></table>";

will output (depending on ID's in database)

Player 1  |  Player 2 |  Score
   p1           p2         0
   p1           p3         0

NOTICE: MySQL_* has been deprecated in PHP 5.5. Use MySQLi or PDO instead

于 2013-03-04T18:02:01.803 回答
0

假设 score1 是玩家 1 的分数:

$query = mysql_query("SELECT * FROM season1 WHERE player1='nickname1'") or die(mysql_error());  

while($row = mysql_fetch_array( $query )) {
echo "<tr><td>"; 
echo "";
echo "</td><td>";   
echo $row['player1']." vs ".$row['player2'].", with a score of: ".$row['score'];
echo "</td></tr>"; 
} 
echo "</table> <br>";

我希望它有所帮助。

萨卢多斯 ;)

于 2013-03-04T18:05:45.227 回答