Adam Projo Adam Projo - 3 months ago 11
HTML Question

Make Looping Table PHP

I want to make looping table in my project.

Here is my code:

<!DOCTYPE html>
<html>
<body>
<table align="left" border="1" cellpadding="3" cellspacing="0">
<?php
for($i=1;$i<=6;$i++)
{
echo "<tr>";
for ($j=1;$j<=5;$j++)
{
echo "<td>$i"</td>";
}
echo "</tr>";
}
?>
</table>
</body>
</html>


I want to make looping table with auto number in the first column. But I get response like this:
enter image description here

Can some one tell me how I can make it only in the first column?

Expected result:

enter image description here

Answer

Then just move your $i outside of inner loop and add empty table data. Reduce your inner loop iteration by 1 count:

<!DOCTYPE html>  
<html>  
<body>  
<table align="left" border="1" cellpadding="3" cellspacing="0">  
<?php  
for($i=1;$i<=6;$i++)  
{  
echo "<tr>";  
echo "<td>$i</td>";  
for ($j=2;$j<=5;$j++)  
  {  
  echo "<td>&nbsp;</td>";  
  }  
  echo "</tr>";  
  }  
?>  
</table>  
</body>  
</html>

Note: Typo in your question - echo "<td>$i"</td>"; to echo "<td>$i</td>";