[英]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.