Reputation: 17
I have a MySQL table:
id name address phone number
16 ek kathmandu 3890
18 rk New york 29304
20 ki Boston 09683
Now I want to print this list in an HTML table like
id name address phonenumber
1 ek kathmandu 3890
2 rk New york 29304
3 ki Boston 09683
using logic
**id name address phonenumber**
<?php foreach ($list as $namelist){?>
$namelist->id $namelist->name $namelist->address $namelist->phone number
<?php}?>
As we can see, $namelist->id
inside foreach
will give direct ID from table, but I want 1 2 3 in the ID. Using for
loop or other loop, I did it, but it didn't work out. How can I sequentially number rows?
Upvotes: 1
Views: 102
Reputation: 3641
You can always use a variable.
If you want to stick with your foreach, it can be something like this
<?php
$counter = 1;
foreach ($list as $namelist){
echo '<tr>';
echo '<td>' + $counter + '</td>';
echo '<td>' + $namelist->name + '</td>';
echo '<td>' + $namelist->address + '</td>';
echo '<td>' + $namelist->phone number + '</td>';
echo '</tr>';
$counter++;
}
?>
or in for loop
for($i=1; $i<=$numofrows; $i++){
echo '<tr>';
echo '<td>' + $counter + '</td>';
echo '<td>' + $namelist->name + '</td>';
echo '<td>' + $namelist->address + '</td>';
echo '<td>' + $namelist->phone number + '</td>';
echo '</tr>';
}
Upvotes: 0
Reputation: 100175
you mean something like:
<?php
$i = 0;
?>
<?php
foreach ($list as $namelist){
...
$i++; //$i will increment with each iteration of foreach
}
?>
Upvotes: 0
Reputation: 67898
So just declare this outside the loop:
$i = 0;
and then inside the loop, before displaying anything, iterate it:
$i++;
and then display it instead.
Upvotes: 1