Reputation: 187
I have generated table using html,mysql and php. I want to generate an extra column and have a number inserted in each row of this new column.
echo "<table border='1'>
<tr>
<th>Rank</th>
<th>owner ID</th>
<th>Total</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . '1' . "</td>";
echo "<td>" . $row['owner_id'] . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
Now the Rank is the extra column I have generated and where the 1 is, I can currently see it in all rows of this new column. How do I get the other rows to be 2,3,4 ect? I have attempted to insert a new row and inserted a 2 after the "'1'." but this just makes the table loose its structure.
Upvotes: 1
Views: 1287
Reputation: 4442
while ($row = mysql_fetch_array($result)) {
@$i++;
echo "<tr>
<td>" . $i . "</td>
</tr>";
}
It is ONLY an alternative, but it's nearly the same thing like above. No need to declare the $i
before. Just use it right inside the loop with a @
in front to prevent an error message. Do it only, if you know the consequences when errors occur.
Upvotes: 0
Reputation: 5512
echo "<table border='1'>
<tr>
<th>Rank</th>
<th>owner ID</th>
<th>Total</th>
</tr>";
$i = 0;
while($row = mysql_fetch_array($result))
{
$i++;
echo "<tr>";
echo "<td>" . $i . "</td>";
echo "<td>" . $row['owner_id'] . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
Upvotes: 2