0

I'm trying to build a simple attendance script which has the members in the users table. I have a script which builds the table and shows me today's date along with the rest of the month.

I also have a script which prints out the individual users, along side these users I want to have a checkbox in every column as far as the dates stretch out to.

I have a foreach statement for printing the users however if I put the <td><input type="checkbox"/></td> into this foreach statement this is only filling in the first column of dates.

If I put it in the for statement which outputs my <th> dates then it appends the checkbox in the <th> which is not what I want.

I'm not the best programmer so I'm not sure of the method that I should be using to achieve this, what I've done is simple so far if you look below you will be able to see how I've achieved this:

To reitrate the problem is that I am unable to append a checkbox per date value from the code below which prints dates from today's date to whatever it is set to.

Any ideas or input gladly welcomed.

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

/* Fetch all of the values of the first column */
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
$startDate = new DateTime();
$endDate = new DateTime('2013-09-31');
$days = array();
 echo "<table>
 <tr>
 <th>Firstname</th>
 <th>Lastname</th>";    
for ($c = $startDate; $c <= $endDate; $c->modify('+1 day')) {
       echo "<th>".$c->format('d')."</th>"; }   
        echo "</tr>";

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

PICTURE 1 SHOWS THE PROBLEM

This is a picture of the problem

PICTURE 2 SHOWS HOW IT SHOULD LOOK

This is how it should look

user0129e021939232
  • 6,205
  • 24
  • 87
  • 140

2 Answers2

2

Here is the solution based on screen shots.

<?php

public function viewall() {

    $sth = $this->db->prepare("SELECT * FROM users");
    $sth->execute();

    /* Fetch all of the values of the first column */
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);
    $startDate = new DateTime();
    $endDate = new DateTime('2013-09-31');

    echo "<table>
     <tr>
     <th>Firstname</th>
     <th>Lastname</th>"; 

    for ($c = clone $startDate; $c <= $endDate; $c->modify('+1 day')) {
        echo "<th>".$c->format('d')."</th>";  
    }
    echo "</tr>";

    foreach($result as $row) {
        echo "<tr>";
        echo "<td>" . $row['firstname'] . "</td>";
        echo "<td>" . $row['lastname'] . "</td>";

        for($c = clone $startDate; $c <= $endDate; $c->modify('+1 day')) {
               echo "<td><input type='checkbox'/></td>";  
        }

        echo "</tr>";
    }

    echo "</table>";

}

?>

EDIT: added clone to copy the object correctly

cmorrissey
  • 8,493
  • 2
  • 23
  • 27
0

I am not very clear about your problem, but I think the following codes may help:

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

  /* Fetch all of the values of the first column */
  $result = $sth->fetchAll(PDO::FETCH_ASSOC);
  $startDate = new DateTime();
  $endDate = new DateTime('2013-09-31');
  $days = array();

  echo "<table>
          <tr>
            <th>Firstname</th>
            <th>Lastname</th>";    
  for ($c = $startDate; $c <= $endDate; $c->modify('+1 day')) 
  {
    echo "<th>" . $c->format('d') . "</th>"; 
  }   
  echo "</tr>";

  foreach ($result as $row) 
  {
    $firstname = $row['firstname'];
    $lastname = $row['lastname'];
    echo "<tr>";
    echo "<td>$firstname</td>";
    echo "<td>$lastname</td>";
    $startDate = new DateTime();
    $endDate = new DateTime('2013-09-31');
    for ($c = $startDate; $c <= $endDate; $c->modify('+1 day')) 
    {
      echo "<td><input type='checkbox'/></td>"; 
    }     
    echo "</tr>";
  }

  echo "</table>";
}

I can not access db, so I cann't test it, what I want to say is that, tr or th stand for a table line, td is children, every line's children's count should be same.

sinfere
  • 927
  • 8
  • 9
  • this didn't work either, the problem is that the checkboxes aren't appearing in the td for every date that is printed out. – user0129e021939232 Sep 18 '13 at 20:01
  • sorry, just got up. I know that's because $startDate should be inited again after it has been the same as $endDate – sinfere Sep 19 '13 at 02:06