I am making a soccer site. I have the following table structure:
season1 (name of the table)
id | player1 | player2 | score
I want to put the scores into a html table. This table is only for player called nickname1 vs every other player. I have this:
$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>";
I tried SELECT * FROM season1 WHERE player1='nickname1' AND player2='nickname2'
but then it will show only player1 vs player2 in the table. I want to show all 4 players vs player1 in the same table.
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
Assuming score1 it's player 1 score:
$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>";
I hope it helps.
Saludos ;)