Reputation: 3
I am trying to make a scoreboard using html/php and MySQL I have the data I need pulled from the DB using a query and Order by table, using a DESC limit of 5 to only show the top 5, although I need a way to be able to list the number 1-5 next to the results. My code is relatively simple and is pasted below.
<?php
mysqli_select_db($connect, $database);;
$sql = "SELECT name,kills FROM global_stats ORDER BY kills DESC LIMIT 5";
$result = mysqli_query($connect, $sql);
$name = $kills = $number = array();
while ($row = mysqli_fetch_array($result)) {
$number = $row['number'];
$name = $row['name'];
$kills = $row['kills'];
echo "<tr> <td>" . $number . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $kills . "</td> </tr>";
}
?>
I basically need where the echo " " . $number . ""; to display the row number from the result, e.g. 1, 2, 3, 4 and 5 I can not use the ID from the database, as this outputs an incorrect number plus this database can not be edited as its managed by another program.
Any help would be appreciated! Thanks guys.
Upvotes: 0
Views: 207
Reputation: 61839
Just add your own counter:
$count = 1;
while ($row = mysqli_fetch_array($result))
{
echo "<tr> <td>" . $count . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['kills'] . "</td> </tr>";
count++;
}
P.S. I removed the redundant variables from your code - you can just use the $row entries directly.
Upvotes: 2