如何使用php在表中显示同一日期以下的出席情况?

如何使用php在表中显示同一日期以下的出席情况?,php,Php,我每月制作一份学生出勤报告。我想在同一日期下显示出席和缺席 <?php foreach ($status as $st): ?> <?php $date = date("d",strtotime($st['ats_date'])); if ($dat['en_id'] == $st['en_id']) { // if ($st['ats_date'] != NULL){ for ($row=1; $row <= 31; $r

我每月制作一份学生出勤报告。我想在同一日期下显示出席和缺席

  <?php foreach ($status as $st): ?>
  <?php
  $date = date("d",strtotime($st['ats_date']));
    if ($dat['en_id'] == $st['en_id']) {
      // if ($st['ats_date'] != NULL){
        for ($row=1; $row <= 31; $row++) {
        if ($row == $date) {
          // code...

        if ($st['ats_status'] == 'Present' ) {

          echo "<th> P </th>";
        }
        elseif ($st['ats_status'] == 'Absent' ) {
          echo "<th> A </th>";
        }
        elseif ($st['ats_status'] == 'Leave' ) {
          echo "<th> L </th>";
        }
        else {
          echo "<th> H </th>";
        }
        }
          // }
          }
      }
     ?>
  <?php endforeach; ?>

我想要这个结果


代码中的一些内容:

1不是每个月都有31天。因此,您应该使用显示为结束编号的月份的最后一天:

$last_day=date't',您正在显示的月份

2标签用于表格标题。用于表格单元格

3至于您的问题,您可以使用一个填充了空表单元格的数组来表示行: $array=array_fill 1,$last_day

然后在代码中,确定从日期开始的月份的日期,将空单元格替换为A、p、L或H


注意:代码未经测试

您在这里循环的数据结构是什么,请给出一个例子。仅供参考,这些不是表头单元格,而是数据单元格-您应该在这里创建td元素,而不是th。您正在为每个$row创建一个表单元格,首先可能应该命名为$col,因为您是从1循环到31,在这里创建列,而不是行值,对吗?但是为什么我在你的截图中没有看到H输出,在状态不是P、A或L的地方?我认为您显示的代码与输出不匹配。
//get last day of month as maximum 
$last_day = date('t', $display-month );

//create empty array from 1 till last_day
$array = array_fill( 1 , $last_day, '<td></td>');

foreach ($status as $st){

    if ($dat['en_id'] == $st['en_id']) {
       //get the date of the record
       $day = date("d",strtotime($st['ats_date']));

       //determine the status
       if ($st['ats_status'] == 'Present' )$status = 'P'
       elseif ($st['ats_status'] == 'Absent' )$status = 'A'
       elseif ($st['ats_status'] == 'Leave' )$status = 'L';
       else $status = 'H';

       //replace the value on the exact day in the array with the status
       $array[ $day ]='<td>'.$status.'</td>';  
       }
//create table row
$student_status='<tr>'.implode('',$student_array).'</tr>';
echo $student_status;