Home » PHP

PHP code to create tables dynamically from user input

Here, we will learn how to generate a table dynamically using PHP code/script by passing number of rows and columns through form?
Submitted by IncludeHelp, on October 15, 2017

By executing this code, we need to enter number of rows (line) and number of columns through HTML form and then PHP script/code will generate the table dynamically according to the given input values.

Here is the PHP Code/Script to generate table randomly

<?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

create table using PHP code





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.