[英]How to display group data based on end date?
I have a table like below:我有一个如下表:
How can i display the data based on task end date with new table.如何使用新表显示基于任务结束日期的数据。 Meaning that, the full <div tag`` will repeat for each ending date and display the records on columns.
这意味着,完整的 <div 标签`` 将在每个结束日期重复并在列上显示记录。 For example-
例如-
**Staff 1**
**Staff 2**
Thanking in anticipation.期待中的感谢。
<div>
<table class="table table-bordered">
<tr>
<th>ID</th>
<th>Task Name</th>
<th>Remarks</th>
<th>Start Date</th>
<th>End Date</th>
</tr>
<?php
$sql = "SELECT * FROM dl_fullremarks ";
$result = mysqli_query($conn, $sql);
foreach ($result as $row) {?>
<tr>
<td><?php echo $row['staff_id']; ?></td>
<td><?php echo $row['task_name']; ?></td>
<td><?php echo $row['task_remarks']; ?></td>
<td><?php echo $row['task_startdate']; ?></td>
<td><?php echo $row['task_enddate']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
You can fetch your data first with SQL and then organize it with a PHP associative array:您可以先使用 SQL 获取数据,然后使用 PHP 关联数组对其进行组织:
$data = [];
foreach ($result as $row) {
// Check if the current end date is part of the array. If not, add it
if (!isset($data[$row['task_enddate']]) {
$data[$row['task_enddate']] = [];
}
// Add the row to the assigned end date
$data[$row['task_enddate']][] = $row;
}
$data
nows contains the following structure: $data
nows 包含以下结构:
[
'2020-11-14' => [
['staff_id' => 1, 'task_name' => 'name', // ...],
['staff_id' => 2, 'task_name' => 'other name', // ...],
],
'2020-12-16' => [
['staff_id' => 3, 'task_name' => 'name', // ...],
],
]
You can now loop through it to display your template:) !您现在可以循环通过它来显示您的模板:) !
foreach ($data as $date => $rows) {
echo '<div>';
echo "<div>$date</div>";
echo '<div class="row">';
foreach ($rows as $row) {
// Display your row data here
}
echo '</div></div>';
}
Group result set by task_endtime
:按
task_endtime
分组结果集:
SELECT * FROM dl_fullremarks
WHERE staff_id = '1'
ORDER BY task_endtime
When task_endtime
changed from last saved then print header.当
task_endtime
从上次保存时更改,然后打印 header。 All other rows are printed as in your example.所有其他行都按照您的示例打印。
$prev_endtime = null
foreach ($result in $rows) {
if (date('Y-m-d', $row['taks_endtime']) != $prev_endtime) {
echo '<div class="header">date formatted here</div>';
$prev_endtime = date('Y-m-d', $row['task_endtime']);
}
echo '<div class="row">row cells gere</div>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.