Double Key Totals PHP Resultset into HTML Table
I've a resultset which sums the totals for N situations and N different codes for a certain period.
Data comes from this SQL
SELECT *, count(fldA) as ca
FROM table
WHERE ('... month/year ...')
GROUP BY Sit, Cod
ORDER BY count(fldA) DESC;
Ok here's the table I want:
+--------------------------------+
| d/l | Sit. A | Sit. B | Sit. N |
+-----+--------+--------+--------+
| c91 | 10 | 05 | 10 |
+-----+--------+--------+--------+
| c93 | 15 | | |
+--开发者_C百科---+--------+--------+--------+
| cN. | 07 | 01 | |
+-----+--------+--------+--------+
| Tot | 22 | 06 | 10 |
+--------------------------------+
So the corresponding array (resultset) for the table above should be something like:
SitA, C93, 15;
SitN, c91, 10;
SitA, c91, 10;
SitA, CN., 07;
SitB, c91, 05;
SitB, cN., 01;
Note that number of situations (Collumns) and codes (Rows) WILL VARY. And there are cases where in the resultset array there should be NO value for some col x row combination.
Obviously, the totals should be calculated afterwards.
The question is... which is the best approach for this type of resultset => table creation? Preferably using only one SQL request.
So, since no help 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;
}
// we gonna use two buffers for collumns and rows...
function fillBuff (&$buff, $fld){
$buff = array();
$sql = "SELECT $fld FROM ouv" . timeFilter() . " GROUP BY $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 as $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 as $l){
$sql="SELECT count(*) AS ca FROM 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 as $r){ //data
$tbl.="<tr>";
$tbl.="<td style='font-size: 50%;'>$r</td>";
foreach($bufftipo as $l){
$sql="SELECT count(*) as ca FROM ouv " . timeFilter();
$sql.= (strpos($sql,"WHERE"))?" AND":" WHERE";
$sql.=" ouvatc='$r' AND 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 the two 'buffers' for collumns and rows setting a sql to calculate each cell at a time...
gl, thx
Paulo Bueno.
精彩评论