Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a dynamic table using php based on users input data

Tags:

php

Currently I am trying to create a daily class routine using PHP. Firstly the site will have seven days name as check box and a text box where users could enter number of class period.

When user will submit the form the code will create another form inside another table (looks like this). And finally user could enter class name, teacher name and the code will store it in database.

But my problem is creating the table dynamically. I can not finding out how to solve this problem.

Any help would be appreciated.

Below you can see what I have tried so far:

<h1>Form two</h1>

<form action="routine_create_process.php" method="POST">
<h3>How many class period do you want to add?</h3>

<input type="text" name="period"/>

<h3>Avalible class day</h3>

<label><input type="checkbox" name="f[]" value="sat" /> Saturday </label>
<br />
<label><input type="checkbox" name="f[]" value="sun"  /> Sunday </label>
<br />
<label><input type="checkbox" name="f[]"  value="mon" /> Monday </label>
<br />
<label><input type="checkbox" name="f[]"  value="tues" /> Tuesday </label>
<br />
<label><input type="checkbox" name="f[]"  value="thurs" /> Wednesday </label>
<br />
<label><input type="checkbox" name="f[]"  value="thus" /> Thursday </label>
<br />
<label><input type="checkbox" name="f[]"  value="fri" /> Friday </label>
<br />
<input type="submit" value="SUBMIT"/>
</form>

My PHP code:

<?php
$period=$_POST['period'];
$arr2=$_POST['f'];
?>

<table border='2'>
<?php 
$count=count($arr2)-1;
for($i=0;$i<=$count;$i++){
  echo "<tr><td>";
  if($i==0){
    echo "<table>";
    echo "<div class='wrap_p'>";
    for($r=1;$r<=$period;$r++){
      echo "<td>";
      echo "<div class='add_css'>";
      echo $r;
      echo "</div></td>";
    }
    echo "</div>";
    echo "</table>";
  }
  echo "</tr><tr><td>"; 
  echo $arr2[$i];
  echo "</td>";
  echo "<td> <input type='text' size='20' /></tr></td>";
}
?>
</table>
like image 377
Mushfiqul Tuhin Avatar asked Jul 31 '26 15:07

Mushfiqul Tuhin


2 Answers

This should solve your problem.

<?php

$arr2=$_POST['f'];
$period = $_POST['period'];

?>

<table border='2'>
<?php 
$count=count($arr2)-1;
?>
<tr><td>&nbsp;</td>
<?php
                for($r=1;$r<=$period;$r++){
                    echo "<td>";
                    echo "<div class='add_css'>";
                    echo $r ;
                    echo "</div></td>";

                }
    for($i=0;$i<=$count;$i++){

echo "<tr><td>"; 

echo $arr2[$i];

        for($r=1;$r<=$period;$r++)
        {
            echo "<td>";
            echo "<div class='add_css'>";
            echo "<input type='text' size='20' />" ;
            echo "</div></td>";
        }

echo "</td>";


}

?>
</table>
like image 179
Choudhury A. M. Avatar answered Aug 03 '26 03:08

Choudhury A. M.


I think this is close to what you need.

<html>
<body>
<?php
$period = 8;
$arr2=array('sat','sun','mon','tue','wed','thu','fri');
?>
<table border='2'>
<tr>
<th>Days</th>
<?php 
for($i=1;$i <= $period; $i++){
    ?><th><?php echo $i; ?></th><?php
}
?>
</tr>
<?php 
foreach($arr2 as $day){
    ?>
    <tr>
        <th><?php echo $day; ?></th>
        <?php
        for($i=1;$i <= $period; $i++){
            ?>
            <td>
                <input type="text" placeholder="subject">
                <input type="text" placeholder="subject code">
                <input type="text" placeholder="teachers name">
            </td>
            <?php
        } 
        ?>
        </tr>
    <?php
}
?>
</table>
</body>
</html>
like image 32
manta Avatar answered Aug 03 '26 04:08

manta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!