SyamAhmad SyamAhmad - 19 days ago 8
PHP Question

Horizontal vs vertical table group by date

I want to generate a html table with data from sql.
I've able to do this so far.

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


Which came out like this.

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


I'm trying to figure out how to print it so that each date will be a new column.

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


Is it possible? How do I do it? I've seen someone code printed horizontally. But it is not something like this. I know it is all about logic here which I'm not very good at. Here I've been trying with
<td><tr>
combination to make date as a table header without so much luck so far.

Answer

I misunderstood your question first, now I think I understand. What you need is to create a multidimensional array with your outputs from your sql and re-organize them after, you can do this with for loops. Check the code under, only you can test it but I can help more if you have problems.

I also made a demo here, with recreated values since I don't have your sql data. This code will work for multiple dates, not just 2 as you posted. Press F9 to run the demo.

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