php html table pivot列标题

时间:2016-01-22 03:11:42

标签: php html-table

我已接近达到我想要的输出,但我有这个问题..

enter image description here

列标题必须是1,2,3,4,5等等。到目前为止,这是我的代码..

<table class="table table-bordered table-condensed" align="center" bordercolor="#CCCCCC">
                            <tr bgcolor="#009933">
                            <td align="center" style="color:#FFF;">Name</td>
                            <td align="center" style="color:#FFF;">Date</td>
                            <td align="center" style="color:#FFF;">Date</td>
                            <td align="center" style="color:#FFF;">Date</td>
                            <td align="center" style="color:#FFF;">Date</td>
                            <td align="center" style="color:#FFF;">Date</td>
                            <td align="center" style="color:#FFF;">Remark</td>
                            </tr>
                            <?php
                            $queryres = mysql_query("SELECT stud_name,
                            MAX(IF(date = '1', remark, '')) AS '1',
                            MAX(IF(date = '2', remark, '')) AS '2',
                            MAX(IF(date = '3', remark, '')) AS '3',
                            MAX(IF(date = '4', remark, '')) AS '4',
                            MAX(IF(date = '5', remark, '')) AS '5',
                            MAX(IF(date = '6', remark, '')) AS '6',
                            MAX(IF(date = '7', remark, '')) AS '7',
                            MAX(IF(date = '8', remark, '')) AS '8',
                            MAX(IF(date = '9', remark, '')) AS '9',
                            MAX(IF(date = '10', remark, '')) AS '10',
                            MAX(IF(date = '11', remark, '')) AS '11',
                            MAX(IF(date = '12', remark, '')) AS '12',
                            MAX(IF(date = '13', remark, '')) AS '13',
                            MAX(IF(date = '14', remark, '')) AS '14',
                            MAX(IF(date = '15', remark, '')) AS '15',
                            MAX(IF(date = '16', remark, '')) AS '16',
                            MAX(IF(date = '17', remark, '')) AS '17',
                            MAX(IF(date = '18', remark, '')) AS '18',
                            MAX(IF(date = '19', remark, '')) AS '19',
                            MAX(IF(date = '20', remark, '')) AS '20', 
                            MAX(IF(date = '21', remark, '')) AS '21',
                            MAX(IF(date = '22', remark, '')) AS '22',
                            MAX(IF(date = '23', remark, '')) AS '23',
                            MAX(IF(date = '24', remark, '')) AS '24',
                            MAX(IF(date = '25', remark, '')) AS '25',
                            MAX(IF(date = '26', remark, '')) AS '26',
                            MAX(IF(date = '27', remark, '')) AS '27',
                            MAX(IF(date = '28', remark, '')) AS '28',
                            MAX(IF(date = '29', remark, '')) AS '29',
                            MAX(IF(date = '30', remark, '')) AS '30',
                            MAX(IF(date = '31', remark, '')) AS '31'
                            FROM tb_attendance WHERE instructor_id = '$inst_id' AND description = '$desc'
                            GROUP BY stud_name");
                            while($result= mysql_fetch_array($queryres)){
                                echo "<tr>";
                                echo "<td class=\"text-center\">".$result['stud_name']."</td>";
                                echo "<td class=\"text-center\">".$result['1']."</td>";
                                echo "<td class=\"text-center\">".$result['2']."</td>";
                                echo "<td class=\"text-center\">".$result['3']."</td>";
                                echo "<td class=\"text-center\">".$result['4']."</td>";
                                echo "<td class=\"text-center\">".$result['5']."</td>";
                                echo "<td class=\"text-center\">"."</td>";
                            }
?>
</table>

是否有办法将(DATE)更改为数字(1,2,3,4,5)并仅显示有记录的数字(备注=存在或缺席)。 例如1 2 3有记录但是4 5没有记录,所以1 2 3只能在表格中显示

1 个答案:

答案 0 :(得分:0)

您需要执行另一个查询以找出表中有哪些日期值:

<table class="table table-bordered table-condensed" align="center" bordercolor="#CCCCCC">
    <tr bgcolor="#009933">
    <td align="center" style="color:#FFF;">Name</td>
<?php
$queryres = mysql_query("SELECT DISTINCT date 
                         FROM tb_attendance 
                         WHERE instructor_id = '$inst_id' AND description = '$desc' 
                         ORDER BY date");
$dates = array();
while ($row = mysql_fetch_assoc($queryres)) {
    $dates[] = $row['date']; // Remember which date columns have values, so we only display those in the student rows.
    echo '<td align="center" style="color:#FFF;">' . $row['date'] . '</td>';
}
?>
<td align="center" style="color:#FFF;">Remark</td>
</tr>
<?php
$queryres = mysql_query("
    SELECT stud_name,
        MAX(IF(date = '1', remark, '')) AS '1',
        MAX(IF(date = '2', remark, '')) AS '2',
        MAX(IF(date = '3', remark, '')) AS '3',
        MAX(IF(date = '4', remark, '')) AS '4',
        MAX(IF(date = '5', remark, '')) AS '5',
        MAX(IF(date = '6', remark, '')) AS '6',
        MAX(IF(date = '7', remark, '')) AS '7',
        MAX(IF(date = '8', remark, '')) AS '8',
        MAX(IF(date = '9', remark, '')) AS '9',
        MAX(IF(date = '10', remark, '')) AS '10',
        MAX(IF(date = '11', remark, '')) AS '11',
        MAX(IF(date = '12', remark, '')) AS '12',
        MAX(IF(date = '13', remark, '')) AS '13',
        MAX(IF(date = '14', remark, '')) AS '14',
        MAX(IF(date = '15', remark, '')) AS '15',
        MAX(IF(date = '16', remark, '')) AS '16',
        MAX(IF(date = '17', remark, '')) AS '17',
        MAX(IF(date = '18', remark, '')) AS '18',
        MAX(IF(date = '19', remark, '')) AS '19',
        MAX(IF(date = '20', remark, '')) AS '20', 
        MAX(IF(date = '21', remark, '')) AS '21',
        MAX(IF(date = '22', remark, '')) AS '22',
        MAX(IF(date = '23', remark, '')) AS '23',
        MAX(IF(date = '24', remark, '')) AS '24',
        MAX(IF(date = '25', remark, '')) AS '25',
        MAX(IF(date = '26', remark, '')) AS '26',
        MAX(IF(date = '27', remark, '')) AS '27',
        MAX(IF(date = '28', remark, '')) AS '28',
        MAX(IF(date = '29', remark, '')) AS '29',
        MAX(IF(date = '30', remark, '')) AS '30',
        MAX(IF(date = '31', remark, '')) AS '31'
    FROM tb_attendance WHERE instructor_id = '$inst_id' AND description = '$desc'
    GROUP BY stud_name");
while($result= mysql_fetch_array($queryres)){
    echo "<tr>";
    echo "<td class=\"text-center\">".$result['stud_name']."</td>";
    foreach ($dates as $date) {
        echo "<td class=\"text-center\">".$result[$date]."</td>"; 
    }
    echo "</tr>";
}