首页 > 解决方案 > 如何使用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; $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; ?>

在此处输入图像描述

我想要这个结果

在此处输入图像描述

标签: php

解决方案


您的代码中有一些内容:

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

$last_day = date('t', THE_MONTH_YOU_ARE_DISPLAYING );

2)<th>标签用于表头。<td>用于表格单元格

3)至于您的问题,您可以使用一个填充了空表单元格的数组作为该行: $array = array_fill( 1 , $last_day, '<td></td>');

然后在您的代码中,根据日期确定月份中的哪一天,用A、P、L 或 H替换空单元格。

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

注意:代码未经测试。


推荐阅读