我想使用 phpmysql制作这样的出勤视图
以下是我的考勤表:
`CREATE TABLE IF NOT EXISTS `attendance` (
`aid` int(11) NOT NULL AUTO_INCREMENT,
`Name_of_Student` varchar(100) COLLATE latin1_general_ci NOT NULL,
`Class` varchar(20) COLLATE latin1_general_ci NOT NULL,
`Roll_no` int(11) NOT NULL,
`Section` varchar(20) COLLATE latin1_general_ci NOT NULL,
`Status` binary(1) NOT NULL,
`time` varchar(20) COLLATE latin1_general_ci NOT NULL,
`Date` date NOT NULL,
PRIMARY KEY (`aid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=33 ;
--
-- Dumping data for table `attendance`
--
INSERT INTO `attendance` (`aid`, `Name_of_Student`, `Class`, `Roll_no`, `Section`, `Status`, `time`, `Date`) VALUES
(1, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Morning', '2013-07-01'),
(2, 'Zonundanga', 'X', 5, 'A', 'Y', 'Morning', '2013-07-01'),
(3, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Morning', '2013-07-02'),
(4, 'Zonundanga', 'X', 5, 'A', 'Y', 'Morning', '2013-07-02'),
(5, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Morning', '2013-07-03'),
(6, 'Zonundanga', 'X', 5, 'A', 'Y', 'Morning', '2013-07-03'),
(7, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Morning', '2013-07-04'),
(8, 'Zonundanga', 'X', 5, 'A', 'Y', 'Morning', '2013-07-04'),
(9, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Morning', '2013-07-05'),
(10, 'Zonundanga', 'X', 5, 'A', 'Y', 'Morning', '2013-07-05'),
(11, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Morning', '2013-07-06'),
(12, 'Zonundanga', 'X', 5, 'A', 'Y', 'Morning', '2013-07-06'),
(13, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Morning', '2013-07-07'),
(14, 'Zonundanga', 'X', 5, 'A', 'Y', 'Morning', '2013-07-07'),
(15, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Morning', '2013-07-08'),
(16, 'Zonundanga', 'X', 5, 'A', 'Y', 'Morning', '2013-07-08'),
(17, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Afternoon', '2013-07-01'),
(18, 'Zonundanga', 'X', 5, 'A', 'Y', 'Afternoon', '2013-07-01'),
(19, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Afternoon', '2013-07-02'),
(20, 'Zonundanga', 'X', 5, 'A', 'Y', 'Afternoon', '2013-07-02'),
(21, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Afternoon', '2013-07-03'),
(22, 'Zonundanga', 'X', 5, 'A', 'Y', 'Afternoon', '2013-07-03'),
(23, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Afternoon', '2013-07-04'),
(24, 'Zonundanga', 'X', 5, 'A', 'Y', 'Afternoon', '2013-07-04'),
(25, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Afternoon', '2013-07-05'),
(26, 'Zonundanga', 'X', 5, 'A', 'Y', 'Afternoon', '2013-07-05'),
(27, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Afternoon', '2013-07-06'),
(28, 'Zonundanga', 'X', 5, 'A', 'Y', 'Afternoon', '2013-07-06'),
(29, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Afternoon', '2013-07-07'),
(30, 'Zonundanga', 'X', 5, 'A', 'Y', 'Afternoon', '2013-07-07'),
(31, 'Lalchhandami', 'X', 1, 'A', 'Y', 'Afternoon', '2013-07-08'),
(32, 'Zonundanga', 'X', 5, 'A', 'N', 'Afternoon', '2013-07-08');`
尝试代码在这里:SQL:
SELECT * FROM attendance
PHP:
$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;
}
$Status = [];
$hming = [];
?>
<table border="1" align="center" width="100%">
<tr>
<th>Name</th>
<?php foreach($column as $dt=>$vt):
$hming[] = $dt;
foreach($vt as $k=>$v):
$Status[$k] = $v['Morning'];
$Status[$k] = $v['Afternoon'];
?>
<th colspan="2"><?php echo $k; ?></th>
<?php endforeach; endforeach; ?>
</tr>
<?php foreach($hming as $name): ?>
<tr>
<td><?php echo $name; ?></td>
<?php foreach($Status as $time): ?>
<td><?php echo $time; ?></td>
<?php endforeach;?>
</tr>
<?php endforeach;?>
</table>
问题是每个学生的日期都没有循环。