Search code examples
phpsqlauto-increment

Adding autonumber column to php table that reads from mysql


I am trying to add a column before my first column that starts numbering each record, starting from 1. I was trying to add autoincrement to this line echo "" . $row['ss'] . "";, but that does not seem to want to work. Any ideas how to do this?

My code looks like this:

$result = mysqli_query($con,"SELECT * FROM `results` WHERE Event='100' AND Gender='M' ORDER BY Performance ASC");

echo "<table border='0'>
<tr>
<th align='left'>Pos</th>
<th align='left'>Event</th>
<th>Performance</th>
<th>Wind</th>
<th>Place</th>
<th align='left'>Name</th>
<th align='left'>Surname</th>
<th>Age</th>
<th>Team</th>
<th>Meet</th>
<th>Date</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

Solution

  • Add this line in header

    <th align='left'>#</th>
    

    And here php code

    $count = 1;
    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $count . "</td>";
    echo "<td>" . $row['ss'] . "</td>";
    echo "<td>" . $row['Event'] . "</td>";
    echo "<td>" . $row['Performance'] . "</td>";
    echo "<td>" . $row['Wind'] . "</td>";
    echo "<td>" . $row['Pos'] . "</td>";
    echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
    echo "<td Width='100'>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Age'] . "</td>";
    echo "<td>" . $row['Team'] . "</td>";
    echo "<td>" . $row['Meet'] . "</td>";
    echo "<td>" . $row['Date'] . "</td>";
    echo "</tr>";
    //other code
    $count=$count+1;
    }