PHP code to create tables dynamically from user input

Here, we will figure out how to produce a table powerfully utilizing PHP code/content bypassing a number of lines and segments through structure?

By executing this code, we have to enter a number of lines (line) and a number of sections through HTML structure and afterwards PHP content/code will produce the table progressively as per the given info values.

Here is the PHP Code/Script to produce table haphazardly

<?php
	$crtable = '';
	if ($_POST){
		$crtable .= '<table border="1">';
		for ($i = 0; $i < $_POST['line']; $i++) {
			$crtable .= '<tr>';
			for ($j = 0; $j < $_POST['colunn']; $j++) {
				$crtable .= '<td width="50">&nbsp;</td>';
			}
			$crtable .= '</tr>';
		}
		$crtable .= '</table>';
	}
?>
<form action="" method="post">
	<table border="0" width="200">
		<tr>
			<td width="80"><label>Column:</label></td>
			<td width="120"><input type="text" name="colunn"></td>
		</tr>
		<tr>
			<td><label>Line:</label></td>
			<td><input type="text" name="line"></td>
		</tr>
		<tr>
			<td colspan="2" align="right">
				<input type="submit" value="Create Table">
			</td>
		</tr>
	</table>
</form>
<br/>
<br/>

<?php
	echo $crtable;
?>

Output:

PHP code to create tables dynamically from user input

Leave a Comment