您必须对数据集进行几次传递才能生成该输出。也就是说,您将拥有表示所有状态值的 4 行,并且您必须对其进行多次迭代以提取日期列标题和“类”行标识符。
您可以在 PHP 中执行此操作。因此,在第一次通过时,您获取标题的日期。并且还存储第一列的“类”。
在第二遍中,您再次遍历数据,但这次它被包裹在一个循环中,因此您可以提取该单元格的记录。
这是一些伪代码:
$records = $db->query("select * from your_query here...");
$dates = [];
$classes = [];
// first pass is to pull out the distinct dates & classes which represent our bounds
foreach($records AS $record) {
$dates[] = $record['execution_date'];
$classes[] = $record['class_name'];
}
// distinct the date set and sort them from lowest to highest
$dates = array_unique($dates);
$dates = sort($dates);
$classes = array_unique($classes);
// display the date row
echo "<tr><td> </td>"
foreach($dates AS $date) {
echo $date;
}
echo "</tr>";
// start displaying each class+date pair
foreach($classes AS $klass) {
echo "<tr>";
echo "<td>" . $klass . "</td>";
// display each date record for this class
foreach($dates AS $date) {
$class_and_date_record = filter($records, $klass, $date);
if($class_and_date_record) {
echo "<td>" . $class_and_date_record['status'] . "</td>";
}
}
echo "</tr>";
}
function filter($records, $klass, $date) {
foreach($records AS $row) {
if($row['class_name'] == $klass && $row['execution_date'] == $date) {
return $row;
}
}
return NULL;
}