0

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

}
2
  • 1
    Between <table> and <th> there is a <tr> missing, as later a </tr>. Commented Sep 18, 2013 at 14:05
  • @Joop this doesn't change it at all, i've tried this Commented Sep 18, 2013 at 14:13

3 Answers 3

5
<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>
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried this but it doesn't work either, this is how I have tried this: <tr><th>Firstname</th> <th>Lastname</th>";for ($c = $startDate; $c <= $endDate; $c->modify('+1 day')) {echo "<th>".$c->format('d')."</th></tr>";array_push($days,$c); } didn't even change it slightly
not to worry adam you are correct, it the way I've formatted my code I've removed the <tr> to a separate echo and this has worked! +1 and will accept the answer shortly
0

The dates you echo immediately; you should concat them too to table.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.