How do you increment a number with every table row?
Try this:
Modify your code as given below:
<tbody>
<?php
$i = 1;
while ($row=mysql_fetch_array($query)) { ?>
<tr>
<td width="5">
<?php
echo $i;
$i++;
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
<?php }?>
</tbody>
how to get automatic row number php and mysql
There are various ways to do that,
One technique is to alter the table
- Alter table
- Add new column
- set it as
AUTO_INCREMENT
And the other is to use session variable
SELECT @rank := @rank+1 As `No`,
Name,
Sex
FROM table1, (SELECT @rank := 0) r
- SQLFiddle Demo Link
Adding autonumber column to php table that reads from mysql
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;
}
How to provide autonumbering to a field in mysql?
First of: why do this? It seems your number sequence is in general a primary key and that means - it has no other meaning that number itself. Therefore, you don't need to worry about gaps in it - and let DBMS handle that for you.
If you still want to rearrange your numbers, you can do this with:
UPDATE t CROSS JOIN (SELECT @i:=0) AS init SET t.number=@i:=@i+1
-but, really, think about this - i.e. if you really need to do that.
Can each row in an HTML table be numbered?
I would go with PHP solution listed by Atul Gupta.
To add more - you also can to start iteration based on which page you are.
<?php
$i = ($page-1) * $itemsPerPage;
while(....)
{
echo $i;
$i++;
}
?>
if you are on the second page of your list would get something like 11,12,13,14,....
Related Topics
How to Remove the Extra Square Bracket from Json Having Multiple Arrays
Cron Job to Delete Files Created Before a Specific Time
Sql Get an Id from a Column Where Ids Separated by Commas
How to Install Extension for PHP Via Docker-Php-Ext-Install
How to Get the Domain Name Without Www, Subdomain, and Com/Net/Org/Etc
How to Install and Use PHP on Macos Via Brew
How to Block Direct Access to My JavaScript Files
Check If a Specific Value Exists At a Specific Key in Any Subarray of a Multidimensional Array
Install PHP Intl Extension on Macos
How to Pass a PHP Variable Using the Url
Curl (7): Failed to Connect to Localhost Port 8000: Connection Refused
Regex for Only Allowing Letters, Numbers, Space, Commas, Periods
Change Language of Site With a HTML Button
Join Category and Subcategory in Same Table