2015-03-30 21 views
-1

我有一個表日期的列表,我想輸出它們使用PHP作爲列名以列標題回顯MySQL結果?在PHP

所以,如果表中有值1,2和3

我想創建一個表將有3列和1,2和3列名

+0

嘗試一些谷歌。 – 2015-03-30 20:40:11

+3

兩者都太寬泛,不清楚你問的是什麼。 – 2015-03-30 20:40:43

+0

我想使用mysql結果作爲列名 – 2015-03-30 20:45:59

回答

0
<?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