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; $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', 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;
注意:代码未经测试。
推荐阅读
- python - 有没有一种pythonic方法可以将整数输入列表(这将是一个字符)转换为整数列表?
- reactjs - Jest + React 可加载错误 不支持
- spring - 如何在春季进行单元测试验证注释
- php - 即使在重新安装服务器后,Wordpress 上的空白页
- python-3.x - 用python求解具有n个未知数的n个方程组
- python - asyncio/aiohttp 不返回响应
- arduino - ArduinoJson DynamicJsonDocument
- python - 如何遍历numpy数组的一个轴,返回内部数组而不是值
- arrays - Laravel 5 合并两个多维数组
- javascript - 如何使用 JavaScript 在 CRM 部分添加创建的元素?