简体   繁体   English

使用PHP回显HTML表

[英]Using PHP to echo a HTML table

I'm creating a user attendance script with PHP, I basically want the structure that looks like the below: 我正在用PHP创建一个用户出勤脚本,我基本上想要的结构如下所示:

Or view my jsFiddle 或查看我的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>

I've got this working dynamically without any database interactions and the day numbers are working correctly with the below code: 我已经在没有任何数据库交互的情况下使它动态地工作了,并且使用下面的代码,日期数字可以正常工作:

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

From this I expanded to introduce database interactions as I want to show the users that are in my database. 由此,我扩展了数据库交互的介绍,因为我想显示数据库中的用户。

The below function shows what I have so far but I've run into a problem 下面的函数显示了我到目前为止所遇到的问题,但是我遇到了一个问题

1. The dynamic function which shows me the dates are not appearing on the same line as the other table headers eg the dates are above the table header firstname. 1.向我显示日期的动态函数未与其他表头出现在同一行上,例如,日期在表头名字的上方。

How can I fix this? 我怎样才能解决这个问题? I haven't worked too much with tables so am not sure what I've done work. 我对表格的工作还不够多,所以不确定我所做的工作。 Any ideas? 有任何想法吗?

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>

needs to become 需要成为

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

The dates you echo immediately; 您立即echo的日期; you should concat them too to table . 你也应该把它们放在table

You echo your datetime before you echo your table 在回显表格之前先回显日期时间

Try to change this code : 尝试更改此代码:

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

into this : 到这个:

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

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

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