multiple checkboxes with php in table

北慕城南 提交于 2020-02-08 03:08:06

问题


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

PICTURE 2 SHOWS HOW IT SHOULD LOOK


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/18877256/multiple-checkboxes-with-php-in-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!