I'm creating a user attendance script with PHP, I basically want the structure that looks like the below:
Or view my 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 e.g. the dates are above the table header firstname.
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>and<th>there is a<tr>missing, as later a</tr>.