表中带有php的多个复选框 [英] multiple checkboxes with php in table

查看:111
本文介绍了表中带有php的多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个简单的出勤脚本,该脚本在users表中具有成员。我有一个脚本来构建表格,并向我显示今天的日期以及该月的剩余时间。

I'm trying to build a simple attendance script which has the members in the users table. I have a script which builds the table and shows me today's date along with the rest of the month.

我还有一个脚本可以打印出各个用户,除了这些用户外,我还希望每个列中都有一个复选框,直到日期可以扩展到为止。

I also have a script which prints out the individual users, along side these users I want to have a checkbox in every column as far as the dates stretch out to.

我有一个foreach语句,用于打印用户,但是如果我输入< td>< input type = checkbox /> < / td> 放入此foreach语句中,仅填充日期的第一列。

I have a foreach statement for printing the users however if I put the <td><input type="checkbox"/></td> into this foreach statement this is only filling in the first column of dates.

如果我将它放在输出我的< th> 日期的for语句中,则它将在< th> 这不是我想要的。

If I put it in the for statement which outputs my <th> dates then it appends the checkbox in the <th> which is not what I want.

我不是最好的程序员,所以我我不确定我应该使用哪种方法来实现这一目标,到目前为止,我所做的事情很简单,如果您看下面的内容,您将能够看到我是如何实现这一目标的:

I'm not the best programmer so I'm not sure of the method that I should be using to achieve this, what I've done is simple so far if you look below you will be able to see how I've achieved this:

要重现此问题,是我无法在下面的代码中为每个日期值添加一个复选框,该复选框无法将今天的日期打印到设置的日期。

欢迎提出任何想法或建议。

Any ideas or input gladly welcomed.

public function viewall() {
$sth = $this->db->prepare("SELECT * FROM users");
$sth->execute();

/* Fetch all of the values of the first column */
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
$startDate = new DateTime();
$endDate = new DateTime('2013-09-31');
$days = array();
 echo "<table>
 <tr>
 <th>Firstname</th>
 <th>Lastname</th>";    
for ($c = $startDate; $c <= $endDate; $c->modify('+1 day')) {
       echo "<th>".$c->format('d')."</th>"; }   
        echo "</tr>";

            foreach($result as $row) {
                $firstname = $row['firstname'];
                $lastname = $row['lastname'];
                  echo "<tr>";
                  echo "<td>$firstname</td>";
                  echo "<td>$lastname</td>";
        }
         echo "<td><input type='checkbox'/></td></tr>";
        echo "</table>";}

图片1显示问题

图2显示应该如何查看

推荐答案

这是基于屏幕截图的解决方案。 / p>

Here is the solution based on screen shots.

<?php

public function viewall() {

    $sth = $this->db->prepare("SELECT * FROM users");
    $sth->execute();

    /* Fetch all of the values of the first column */
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);
    $startDate = new DateTime();
    $endDate = new DateTime('2013-09-31');

    echo "<table>
     <tr>
     <th>Firstname</th>
     <th>Lastname</th>"; 

    for ($c = clone $startDate; $c <= $endDate; $c->modify('+1 day')) {
        echo "<th>".$c->format('d')."</th>";  
    }
    echo "</tr>";

    foreach($result as $row) {
        echo "<tr>";
        echo "<td>" . $row['firstname'] . "</td>";
        echo "<td>" . $row['lastname'] . "</td>";

        for($c = clone $startDate; $c <= $endDate; $c->modify('+1 day')) {
               echo "<td><input type='checkbox'/></td>";  
        }

        echo "</tr>";
    }

    echo "</table>";

}

?>

编辑:添加了 clone 复制对象正确

added clone to copy the object correctly

这篇关于表中带有php的多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆