No-brainer mysql->html tables - sample code

No-brainer mysql->html tables - sample code

am 22.11.2006 05:00:07 von Larry Anderson

Here is a no-brainer table creation I just worked up - you specify the
columns in your MySQL query and the PHP script takes it from there
(creates the heading row, displays the data, then sums the rows.
(dbconnect and dbquery are just functions that do the normal mysql
connect and query). I left in my query as it is also a good example of
grouping)

- build your query in the preferred column output order of your table,
use the AS option to make the header names, '_ 's are replaced with /> tags in the heading row.
- caution - make sure you have safe data, it does utilize "eval"

Larry

dbconnect();

$reptdate = 20061012;

$result = dbquery("SELECT
`facility_loccity` as `city`,
SUM(IF(`facility_type`='sfcch',1,0)) AS `small_homes`,
SUM(IF(`facility_type`='sfcch',`facility_fcchcap`,0)) AS `sm_home_cap`,
SUM(IF(`facility_type`='lfcch',1,0)) AS `large_homes`,
SUM(IF(`facility_type`='lfcch',`facility_fcchcap`,0)) AS `lg_home_cap`,
SUM(IF((`facility_type`='lcent' OR facility_type='excent'),1,0)) AS
`centers`,
SUM(`facility_infcap`) AS `infant_cap`,
SUM(`facility_precap`) AS `presch_cap`,
SUM(`facility_sacap`) AS `schage_cap`,
SUM(`facility_infptvac`) AS `inf_pt_vac`,
SUM(`facility_infftvac`) AS `inf_ft_vac`,
SUM(`facility_preptvac`) AS `presch_pt_vac`,
SUM(`facility_preftvac`) AS `presch_ft_vac`,
SUM(`facility_saptvac`) AS `schage_pt_vac`,
SUM(`facility_saftvac`) AS `schage_ft_vac`,
SUM(`facility_infptenrol`) AS `inf_pt_enrol`,
SUM(`facility_infftenrol`) AS `inf_ft_enrol`,
SUM(`facility_preptenrol`) AS `pre_pt_enrol`,
SUM(`facility_preftenrol`) AS `pre_ft_enrol`,
SUM(`facility_saptenrol`) AS `schage_pt_enrol`,
SUM(`facility_saftenrol`) AS `schage_ft_enrol`
FROM facility
WHERE `facility_begin` <= $reptdate
AND `facility_end` >= $reptdate
AND `facility_type` != 'exhome'
GROUP BY UPPER(`facility_loccity`)
ORDER BY UPPER(`facility_loccity`)");

$first = true;

$line1 = $line3 = 'echo "';

echo '

';

while( $item = mysql_fetch_array($result) ){

if($first){
foreach( $item as $name => $value) {
if(!is_numeric($name)){
echo "";
$line1 .= '';
$line2 .= '$t'.$name.' += $item[\''.$name.'\'];';
$line3 .= '';
}
}
$line1 .= '\r";';
$line3 .= '\r";';
echo "\r";

}

$first = false;
eval($line1);
eval($line2);
}
eval($line3);
echo '
".strtoupper(str_replace("_","
",$name))."
".$item[\''.$name.'\']."".$t'.$name.'."
';
?>