dongxingqiu7943
2016-01-22 03:11
浏览 29
已采纳

php html table pivot列标题

I am close of reaching my desired output, but I have this problem..

enter image description here

The column header must be 1,2,3,4,5 etc. etc. and here is my code so far..

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

Is there a way to change (DATE) into the numbers (1,2,3,4,5) and only show the numbers where there is a record(remark = Present or Absent). for example 1 2 3 have a records but 4 5 has nothing so 1 2 3 must only be shown in the table

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushi9444 2016-01-22 03:58
    已采纳

    You'll need to do another query to find out which dates have values in the table:

    <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>";
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题