0

我有一段简单的代码可以让我回显特定月份的开始和结束日期,我想做的是创建一个注册系统。

我将表格中的日期作为表格标题,然后在第一列中我有成员名称。我想要实现的是每天的复选框或单选元素,但我正在努力实现这一点我没有得到预期的结果,而是我得到了这个:

2013-10-01 13:44:213Europe/Berlin 2013-10-01 13:44:213Europe/Berlin

由此:

 <?php  
 $dt = "<td><input type='checkbox' name='student[davidsmith]' value='Y' checked /></td>";
 foreach($startDate as $dt){
 echo "$dt";
} ?>

我觉得我很可能错过了这样做的重点和方法。也许有一种更简洁更简单的方法来实现我想要实现的目标。(目前我没有数据库交互,我真的想对框架进行最初的排序)。

如果有人可以帮助我完成这个,那就太好了!

日期.php

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Attendance Example</title>
</head>

<body>

<form action='this_page.php' method='post'>
<table>
<th>Member</th>
<?php 
$startDate = new DateTime();
$endDate = new DateTime('2013-09-31');

for ($c = $startDate; $c <= $endDate; $c->modify('+1 day')) {
       echo "<th>".$c->format('d')."</th>"; }
 ?>
<tr>
    <td>Memeber One</td>
    <td><input type='checkbox' name='student[davidsmith]' value='Y' /></td>
     <?php  
     $dt = "<td><input type='checkbox' name='student[davidsmith]' value='Y' checked /></td>";
     foreach($startDate as $dt){
     echo "$dt";
   } ?>

</tr>
<tr>
    <td>Member Two</td>
 <?php  
     $dt = "<td><input type='checkbox' name='student[davidsmith]' value='Y' checked /></td>";
     foreach($c as $dt){
     echo "$dt";
   } ?>            <td><input type='checkbox' name='student[davidsmith]' value='1' /></td>
</tr>
</table>
</form>
</body>
</html>
4

1 回答 1

2

对您的代码的评论:

你得到你发布的结果是因为:

  1. $startDateinforeach($startDate as $dt)不是数组,因此没有循环
  2. 当您说$dt要覆盖 $dt 变量$dt = "<td><input type='checkbox' name='student[davidsmith]' value='Y' checked /></td>";时,就好像它从未存在过一样

我的解决方案:
现在,如果我理解正确,我相信这是您正在寻找的代码

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Attendance Example</title>
</head>

<body>

<form action='this_page.php' method='post'>
<table>
<th>Member</th>
<?php 
$startDate = new DateTime();
$endDate = new DateTime('2013-09-31');
$days = array();

for ($c = $startDate; $c <= $endDate; $c->modify('+1 day')) {
       echo "<th>".$c->format('d')."</th>";array_push($days,$c); }
 ?>
<tr>
    <td>Memeber One</td>

     <?php  

     foreach($days as $dt){
     echo '<td><input type="checkbox" name="student[davidsmith]" value="'.$dt->format('d') .'" /></td>';
   } ?>

</tr>
<tr>
    <td>Member Two</td>
 <?php  
      foreach($days as $dt){
     echo '<td><input type="checkbox" name="student[davidsmith]" value="'.$dt->format('d') .'" /></td>';
   } ?>
</tr>
</table>
</form>
</body>
</html>

首先,我们将日期放入一个数组中,然后循环它们并为每一天创建复选框。每个复选框都应具有与其所代表的日期相对应的值。希望这就是你要找的。

于 2013-09-17T12:42:14.717 回答