【问题标题】:Horizontal vs vertical table group by date [closed]按日期分组的水平与垂直表[关闭]
【发布时间】:2016-11-20 07:16:04
【问题描述】:

我想用来自 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        |

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

【问题讨论】:

    标签: php html-table


    【解决方案1】:

    我首先误解了你的问题,现在我想我明白了。 您需要使用 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>
    

    【讨论】:

    • 我已经尝试了您的建议,但在第二列的新 td 上结束了第一个日期。其余的仍在第一列。
    • @SyamAhmad,我重新回答了。如果你明白了,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2019-11-28
    • 2016-09-14
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2022-07-12
    相关资源
    最近更新 更多