且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 php 和 mysql 创建出勤数据透视表

更新时间:2023-02-05 15:55:17

我的问题的更正解决方案在这里:

The corrected solution to my question is here:

$query = $dbh->query("SELECT * FROM attendance");
$result = $query->fetchAll(PDO::FETCH_OBJ);
$column = [];
foreach($result as $key=>$val){
    $column[$val->Name_of_Student][$val->Date][$val->time] = $val->Status; 
}
$dateat = [];
foreach($column as $studentname=>$hming){   
    foreach($hming as $dt=>$nihming){
        $dateat[$studentname][] = $dt;
    }
}
$prevDate = []; 
?>
<table border="1" align="center" width="80%" cellpadding="10" cellspacing="4">
    <?php 
        echo "<tr>";
        echo "<td>Name</td>";
        foreach($dateat as $nk=>$nithar):
                foreach($nithar as $atdate):                    
                    if (!in_array($atdate, $prevDate)) {
                        $prevDate[] = $atdate;
                        echo '<td colspan="2">'.$atdate."</td>\n";
                    }
                endforeach; 
            endforeach; 
        echo "</tr>";
    foreach($column as $stname=>$stval):
            echo "<tr>";
            echo "<td>".$stname."</td>\n";
            foreach($stval as $sk=>$sv){
                foreach($sv as $stt){
                    echo "<td>".$stt."</td>\n";
                }
            }
            echo "</tr>";
    endforeach; ?>
</table>

输出:

<table border="1" align="center" width="80%" cellpadding="10" cellspacing="4">
	<tr><td>Name</td><td colspan="2">2013-07-01</td>
<td colspan="2">2013-07-02</td>
<td colspan="2">2013-07-03</td>
<td colspan="2">2013-07-04</td>
<td colspan="2">2013-07-05</td>
<td colspan="2">2013-07-06</td>
<td colspan="2">2013-07-07</td>
<td colspan="2">2013-07-08</td>
</tr><tr><td>Lalchhandami</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
</tr><tr><td>Zonundanga</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>N</td>
</tr></table>