Php 按日期分组的水平表与垂直表
我想用sql中的数据生成一个html表。 到目前为止,我能做到这一点Php 按日期分组的水平表与垂直表,php,html-table,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">
<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|2013-08-18|
|yes |no |
|no |no |
|yes |yes |
|yes |yes |
|no |no |
可能吗?我该怎么做?我见过有人水平打印代码。但不是这样的。我知道这都是逻辑问题,我不太擅长。在这里,我一直尝试使用
组合将日期作为表格标题,但迄今为止运气不佳。我首先误解了你的问题,现在我想我明白了。
您需要的是使用sql的输出创建一个多维数组并重新组织它们,之后,您可以使用for循环来执行此操作。检查下面的代码,只有您可以测试它,但如果您有问题,我可以提供更多帮助
我还制作了一个演示,其中包含重新创建的值,因为我没有您的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,我重新做了我的回答。如果你明白了,请告诉我。
<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>