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

2
  • can you show us (screen shot) what you want it to look like so we can help Commented Sep 18, 2013 at 16:21
  • @ChristopherMorrissey done! i've also showed you how i would like it too look Commented Sep 18, 2013 at 16:28

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

no that didn't work oddly enough I was certain that would work as it makes sense but it didn't echo out the checkboxes not even one
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.

2 Comments

this didn't work either, the problem is that the checkboxes aren't appearing in the td for every date that is printed out.
sorry, just got up. I know that's because $startDate should be inited again after it has been the same as $endDate

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.