Creating a dynamic table using php based on users input data

社会主义新天地 提交于 2019-12-18 08:58:08

问题


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>

回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/21809991/creating-a-dynamic-table-using-php-based-on-users-input-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!