Home >>PHP Tutorial >Create table using for loop in PHP

Create table using for loop in PHP

Create table using for loop in PHP

<?php

if(isset($_POST['create']))

{

$rows=$_POST['r'];

$cols=$_POST['c'];

echo "<table border='1'>";

for($i=0;$i<$rows;$i++)

{

echo "<tr>";

for($j=0;$j<$cols;$j++)

{

echo "<th>"."r".$i."c".$j."</th>";

}

echo "</tr>";

}

echo "</table>";

}

?>

<html>

<body>

<form method="post">

<table width="400" border="1">

<tr>

<td width="177">Enter number of rows </td>

<td width="207"><input type="text" name="r"/></td>

</tr>

<tr>

<td>Enter number of column </td>

<td><input type="text" name="c"/></td>

</tr>

<tr>

<td colspan="2">

<input type="submit" value="Create Table" name="create"/>

</td>

</tr>

</table>

</form>

</body>

</html>
Output
r0c0 r0c1 r0c2
r1c0 r1c1 r1c2
r2c0 r2c1 r2c2
Enter number of rows
Enter number of column

No Sidebar ads