繁体   English   中英

使用PHP回显HTML表

[英]Using PHP to echo a HTML table

我正在用PHP创建一个用户出勤脚本,我基本上想要的结构如下所示:

或查看我的jsFiddle

<form action='this_page.php' method='post'>
<table>
    <th>Member</th>
    <th>Day One</th>
    <th>Day Two</th>
    <tr>
        <td>Memeber One</td>
        <td><input type='checkbox' name='student[davidsmith]' value='1' /></td>
                <td><input type='checkbox' name='student[davidsmith]' value='1' /></td>
    </tr>
    <tr>
        <td>Member Two</td>
        <td><input type='checkbox' name='student[davidsmith]' value='1' /></td>
                <td><input type='checkbox' name='student[davidsmith]' value='1' /></td>
    </tr>
</table>
</form>

我已经在没有任何数据库交互的情况下使它动态地工作了,并且使用下面的代码,日期数字可以正常工作:

<?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); }
 ?>

由此,我扩展了数据库交互的介绍,因为我想显示数据库中的用户。

下面的函数显示了我到目前为止所遇到的问题,但是我遇到了一个问题

1.向我显示日期的动态函数未与其他表头出现在同一行上,例如,日期在表头名字的上方。

我怎样才能解决这个问题? 我对表格的工作还不够多,所以不确定我所做的工作。 有任何想法吗?

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


    /* Fetch all of the values of the first column */
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);

            $table = "<form action='' method='post'>
            <table>
                <th>Firstname</th>
                <th>Lastname</th>";
                $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); }

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

}
<table>
<th>Member</th>
<th>Day One</th>
<th>Day Two</th>

需要成为

<table>
  <tr>
    <th>Member</th>
    <th>Day One</th>
    <th>Day Two</th>
  </tr>

您立即echo的日期; 你也应该把它们放在table

在回显表格之前先回显日期时间

尝试更改此代码:

echo "<th>".$c->format('d')."</th>";array_push($days,$c);

到这个:

$table.="<th>".$c->format('d')."</th>";array_push($days,$c);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM