php 基于用户输入数据使用php创建动态表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21809991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:27:45  来源:igfitidea点击:

Creating a dynamic table using php based on users input data

php

提问by Mushfiqul Tuhin

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.

目前我正在尝试使用 PHP 创建日常课程。首先,该站点将有 7 天名称作为复选框和一个文本框,用户可以在其中输入课时数。

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代码:

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

采纳答案by Choudhury A. M.

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>

回答by manta

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>