【发布时间】:2015-03-30 20:35:16
【问题描述】:
我有一个表格中的日期列表,我想使用 php 作为列名输出它们
所以如果表的值是 1,2 和 3
我想创建一个包含 3 列和 1,2 和 3 作为列名的表
【问题讨论】:
-
既太宽泛又不清楚你在问什么。
-
我想用mysql结果作为列名
我有一个表格中的日期列表,我想使用 php 作为列名输出它们
所以如果表的值是 1,2 和 3
我想创建一个包含 3 列和 1,2 和 3 作为列名的表
【问题讨论】:
<?php
function db_select_with_col()
{
$rows = array();
$result = db_query("SELECT
student.StudentID,
student.`Name`,
attendance.Date,
CASE WHEN attendance.StudentID IS NOT NULL THEN 'Present' ELSE 'Absent' END as Attendance_Status
FROM
student
Left JOIN attendance ON student.StudentID = attendance.StudentID
WHERE
attendance.Date = '2015-03-30'");
// If query failed, return `false`
if ($result === false) {
return false;
}
// If query was successful, retrieve all the rows into an array
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$colNames = array_keys(reset($rows));
foreach ($colNames as $colName) {
echo "<th>$colName</th>";
}
foreach ($rows as $row) {
echo "<tr>";
foreach ($colNames as $colName) {
echo "<td>" . $row[$colName] . "</td>";
}
echo "</tr>";
}
}
?>
感谢 [这里] (https://stackoverflow.com/a/14430366/4680117) 的回答,我想出了如何做到这一点
【讨论】: