javascript - Double Key Totals PHP Resultset into HTML Table -
i've resultset wich sums totals n situations , n different codes period.
data comes sql
select *, count(flda) ca table ('... month/year ...') group sit, cod order count(flda) desc;
ok here's table want:
+--------------------------------+ | d/l | sit. | sit. b | sit. n | +-----+--------+--------+--------+ | c91 | 10 | 05 | 10 | +-----+--------+--------+--------+ | c93 | 15 | | | +-----+--------+--------+--------+ | cn. | 07 | 01 | | +-----+--------+--------+--------+ | tot | 22 | 06 | 10 | +--------------------------------+
so corresponding array (resultset) table above should like:
sita, c93, 15; sitn, c91, 10; sita, c91, 10; sita, cn., 07; sitb, c91, 05; sitb, cn., 01;
note number of situations (collumns) , codes (rows) will vary. , there cases in resultset array there should no value col x row combination.
obviously, totals should calculated afterwards.
the question is... wich best aproach type of resultset => table creation? prefeably using 1 sql request.
thx
so, since no comes... i've done this
<?php
...
$sql=""; function timefilter(){ $q=""; if ($_get['mes']!='' || $_get['ano']!=''){ $q.=" where";} if ($_get['mes']!=''){ $q.=" month(ouventrada)=$_get[mes]";} if ($_get['mes']!='' && $_get['ano']!=''){ $q.=" and";} if ($_get['ano']!=''){$q.=" year(ouventrada)=$_get[ano]";} return $q; } // gonna use 2 buffers collumns , rows... function fillbuff (&$buff, $fld){ $buff = array(); $sql = "select $fld ouv" . timefilter() . " group $fld"; $res = mysql_query($sql); while ($row=mysql_fetch_assoc($res)){ $buff[]=$row[$fld]; } } $quad .= "<div id='stats'>"; // fill buffers $buffatc = array(); $bufftipo = array(); fillbuff ($buffatc, 'ouvatc'); fillbuff ($bufftipo, 'ouvtipo'); // table headers $tbl = "<table class='resumo'>"; $tbl.= "<thead><tr><th></th>"; foreach ($bufftipo $l){ $tbl.="<th>$l</th>"; } $tbl.="</tr></thead>"; // table foot (totals) $tbl.="<tfoot><tr style='background-color: #f0f0f0'><td style='font-size: 50%;'>total</td>"; foreach($bufftipo $l){ $sql="select count(*) ca ouv" . timefilter(); $sql.= (strpos($sql,"where"))?" and":" where"; $sql.=" ouvtipo='$l'"; $rowt = mysql_fetch_assoc(mysql_query($sql)); $tbl.="<td style='font-weight: bold'>$rowt[ca]</td>"; } $tbl.="</tfoot>"; // table body $tbl.="<tbody>"; foreach ($buffatc $r){ //data $tbl.="<tr>"; $tbl.="<td style='font-size: 50%;'>$r</td>"; foreach($bufftipo $l){ $sql="select count(*) ca ouv " . timefilter(); $sql.= (strpos($sql,"where"))?" and":" where"; $sql.=" ouvatc='$r' , ouvtipo='$l'"; $rowt = mysql_fetch_assoc(mysql_query($sql)); $tbl.="<td style='font-weight: bold'>$rowt[ca]</td>"; } $tbl.="</tr>"; } $tbl.="</tbody>"; $tbl .="</table>"; $quad.= $tbl; $quad.= "</div>";
we interact throught 2 'buffers' collumns , rows setting sql calculate each cell @ time...
gl, thx
paulo bueno.
Comments
Post a Comment