2013-08-03 29 views
-3

我想用sql中的數據生成一個html表。 我已經能夠做到這一點。水平vs垂直表按日期分組

<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>組合將date作爲表頭,但迄今爲止沒有那麼多運氣。

回答

0

我誤解了你的問題,現在我想我明白了。 你需要的是用你的sql的輸出創建一個多維數組,然後重新組織它們,你可以用for循環來完成。檢查下的代碼,只有你可以測試它,但如果你有問題,我可以幫助更多。

我也做了演示here,重新創建值,因爲我沒有你的SQL數據。此代碼適用於多個日期,而不僅僅是您發佈的日期。按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> 
+0

我已經嘗試了您的建議,但最終在第二列新td上的第一個日期。其餘的仍然在第一欄。 – SyamAhmad

+0

@SyamAhmad,我重新做了我的回答。如果你瞭解它,讓我知道。 – Sergio