--usage1----------
$sql = '';
$sql.="SELECT fieldtype, count(1) as count ";
$sql.="FROM mytable ";
$sql.="GROUP BY fieldtype ";
$sql.="ORDER BY fieldtype ";
echo report_append("summary of data", $sql);
--output1---------
|fieldtype|count|
|0 |9 |
|1 |397 |
|2 |965 |
|3 |554 |
|4 |683 |
|5 |1352 |
|6 |1390 |
|7 |237 |
|8 |1372 |
--usage2----------
$q='';
$q.="SELECT
(case
when fieldtype=1 then 'Apple'
when fieldtype=2 then 'Banana'
when fieldtype=3 then 'Blackberry'
when fieldtype=4 then 'Cherry'
when fieldtype=5 then 'Grape'
when fieldtype=6 then 'Orange'
when fieldtype=7 then 'Peach'
when fieldtype=8 then 'Pear'
else '' end) as typename, fieldtype, count(1) as count
FROM mytable d GROUP BY fieldtype ORDER by fieldtyped;
";
report_append("summary of data", $q);
--output2---------
|typename |fieldtype|count|
| |0 |9 |
|Apple |1 |397 |
|Banana |2 |965 |
|Blackberry|3 |554 |
|Cherry |4 |683 |
|Grape |5 |1352 |
|Orange |6 |1390 |
|Peach |7 |237 |
|Pear |8 |1372 |
--code------------
function select_all($sql)
{
$rows = array();
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
$rows[] = $row;
return $rows;
}
function report_append($title,$sql)
{
$rows = select_all($sql);
$headersize=array();
$header=array();
foreach($rows[0] as $colname=>$col)
$header[]=$colname;
foreach($rows[0] as $colname=>$col)
$headersize[] = strlen($colname);
foreach($rows as $row)
foreach($headersize as $colnum=>$col)
$headersize[$colnum] = max( $headersize[$colnum] , strlen($row[$header[$colnum]]) );
$h='';
foreach($header as $colnum=>$colname)
$h.="|".sprintf("%-".$headersize[$colnum]."s", $colname);
$h.= "|\n";
foreach($rows as $row)
{
foreach($headersize as $colnum=>$col)
$h.="|".sprintf("%-".$col."s", $row[$header[$colnum]]);
$h.= "|\n";
}
$return='';
$return.="---------------------------------------------------------\n";
$return.="$title\n";
$return.="---------------------------------------------------------\n";
$return.=$h;
return $return;
}