按日期水平与垂直表组

时间:2013-08-03 00:51:12

标签: php html-table

我想用sql中的数据生成一个html表。 到目前为止我能做到这一点。

<div class="row-fluid">
    <!-- BEGIN EXAMPLE TABLE PORTLET-->
    <div class="portlet box blue">
        <div class="portlet-title">
            <div class="caption">
                <i class="icon-edit"></i>Attendance
            </div>
        </div>
        <div class="portlet-body">
            <div class="clearfix">
            </div>
            <?php echo "<form id='insertAtt' action='insertAtt2.php' method='post'>
            " ?>
            <table class="table table-striped table-hover table-bordered" id="studAtt">
            <tbody>
            <?php
$result = mysqli_query($con," SELECT date,t1.studID,studName,Class,attendance FROM mate_student as t1, mate_student_att as t2 WHERE t1.studID=t2.studID ORDER BY date"); 
                        $date = '';
$att = '';
$prevDate='';
  while($row = mysqli_fetch_assoc($result)){
    $curDate= '<td>
            '.$row['date'].'
        </td>
        '; $att= '
        <tr>
            <td>
                '.$row['attendance'].'
            </td>
        </tr>
        '; if( $curDate!=$prevDate) {echo $curDate; } echo $att; $prevDate=$curDate; } ?>
    </div>
    <!-- END EXAMPLE TABLE PORTLET-->
</div>
</div>
<!-- END PAGE CONTENT -->
</div>
<!-- END PAGE CONTAINER-->
</div>

这就是这样的。

|2013-08-17|
|yes       |
|no        |
|yes       |
|yes       |
|no        |
|2013-08-18|
|no        |
|no        |
|yes       |
|yes       |
|no        |

我正在试图找出如何打印它,以便每个日期都是一个新列。

|2013-08-17|2013-08-18|
|yes       |no        |
|no        |no        |
|yes       |yes       |
|yes       |yes       |
|no        |no        |

有可能吗?我该怎么做?我见过有人代码水平打印。但它不是这样的。我知道这是关于逻辑的,我不是很擅长。在这里,我一直在尝试使用<td><tr>组合将日期作为表格标题,目前为止没有那么多运气。

1 个答案:

答案 0 :(得分:0)

我首先误解了你的问题,现在我想我理解了。 你需要的是用你的sql输出创建一个多维数组,然后重新组织它们,你可以用for循环来完成。检查下面的代码,只有你可以测试它,但如果你有问题,我可以帮助更多。

我还制作了一个演示 here ,并重新创建了值,因为我没有你的sql数据。此代码适用于多个日期,而不仅仅是您发布的2个日期。按F9运行演示。

<div class="row-fluid">
    <!-- BEGIN EXAMPLE TABLE PORTLET-->
    <div class="portlet box blue">
        <div class="portlet-title">
            <div class="caption">
                <i class="icon-edit"></i>Attendance
            </div>
        </div>
        <div class="portlet-body">
            <div class="clearfix">
            </div>
            <?php echo "<form id='insertAtt' action='insertAtt2.php' method='post'>" ?>
            <table class="table table-striped table-hover table-bordered" id="studAtt">
            <tbody>
            <?php
                $result = mysqli_query($con," SELECT date,t1.studID,studName,Class,attendance FROM mate_student as t1, mate_student_att as t2 WHERE t1.studID=t2.studID ORDER BY date"); 
                                        $date = '';
                $att = '';
                $prevDate='';
                $tbi = -1;
                $table_array = array();
                while($row = mysqli_fetch_assoc($result)){

                    $curDate= '<td>
                            '.$row['date'].'
                        </td>
                        '; 
                    $att.= ',
                        <tr>
                            <td>
                                '.$row['attendance'].'
                            </td>
                        </tr>
                        '; 
                    if( $curDate!=$prevDate) {
                        $tbi++;
                        $table_array[$tbi][]= $curDate;
                    }  
                    $table_array[$tbi][] = $att;
                } 
                            $trow='';
                for ($x = 0; $x < count($table[0]); $x++) {

                    $trow.='<tr>';
                    for ($ti = 0; $ti < count($table); $ti++) {
                        $trow.='<td>'.$table[$ti][$x].'</td>';
                    }
                    $trow.='</tr>';

                }
                echo $trow;
            ?>
        </tbody>
        </table>
    </div>
    <!-- END EXAMPLE TABLE PORTLET-->
</div>
</div>
<!-- END PAGE CONTENT -->
</div>
<!-- END PAGE CONTAINER-->
</div>