How Do I Create Tables Dynamically From User Input?
Want I want to do is create a table with what the user input for rows and columns, but I don't understand how to do this. Ex. User inputs: number of rows: 4 number of columns: 5
Solution 1:
Once you get your input data with GET or POST method and then you assign them to variables like $rows
and $cols
. you can do this
$cols = 5;
$rows = 2;
$table = "<table>";
for($i=0;$i<$rows;$i++)
{
$table .= "<tr>";
for($j=0;$j<$cols;$j++)
$table .= "<td> Content </td>";
$table .= "</tr>";
}
$table .= "</table>";
echo$table;
Solution 2:
In Php get the input values and and generate table using loop... here is a complete code ... check it..
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Dynamic Table</title></head><body><formaction=""method="get">
ROWS <inputtype="text"name="rows"> COLUMNS <inputtype="text"name="cols"><inputtype="submit"value="Generate"></form><?php// Just a check you can change as your needsif(isset($_GET['rows'])){
$rows=$_REQUEST['rows'];
$cols=$_REQUEST['cols'];
echo'<table border="1">';
for($row=1;$row<=$rows;$row++){
echo'<tr>';
for($col=1;$col<=$cols;$col++){
echo'<td> sample value </td>';
}
echo'</tr>';
}
echo'</table>';
}
?></body></html>
Solution 3:
Copy and paste into a php page, place the column and lines in text fields click the Create Table button, keep testing and looking at the source code that one day you'll understand.
<?php$table = '';
if ($_POST) {
$table .= '<table border="1">';
for ($i = 0; $i < $_POST['qty_line']; $i++) {
$table .= '<tr>';
for ($j = 0; $j < $_POST['qty_colunn']; $j++) {
$table .= '<td width="50"> </td>';
}
$table .= '</tr>';
}
$table .= '</table>';
}
?><formaction=""method="post"><tableborder="0"width="200"><tr><tdwidth="80"><label>Column</label></td><tdwidth="120"><inputtype="text"name="qty_colunn"></td></tr><tr><td><label>Line</label></td><td><inputtype="text"name="qty_line"></td></tr><tr><tdcolspan="2"align="right"><inputtype="submit"value="Create Table"></td></tr></table></form><br /><br /><?phpecho$table;
?>
Post a Comment for "How Do I Create Tables Dynamically From User Input?"