32 $aggfield =
false,$sumlabel=
'Sum ',$aggfn =
'SUM', $showcount =
true)
34 if ($aggfield) $hidecnt =
true;
35 else $hidecnt =
false;
37 $iif = strpos($db->databaseType,
'access') !==
false;
42 if ($where) $where =
"\nWHERE $where";
43 if (!is_array($colfield)) $colarr = $db->GetCol(
"select distinct $colfield from $tables $where order by 1");
44 if (!$aggfield) $hidecnt =
false;
46 $sel =
"$rowfields, ";
47 if (is_array($colfield)) {
48 foreach ($colfield as $k => $v) {
52 "\n\t$aggfn(IIF($v,1,0)) AS \"$k\", " 54 "\n\t$aggfn(CASE WHEN $v THEN 1 ELSE 0 END) AS \"$k\", ";
58 "\n\t$aggfn(IIF($v,$aggfield,0)) AS \"$sumlabel$k\", " 60 "\n\t$aggfn(CASE WHEN $v THEN $aggfield ELSE 0 END) AS \"$sumlabel$k\", ";
64 foreach ($colarr as $v) {
65 if (!is_numeric($v)) $vq = $db->qstr($v);
68 if (strlen($v) == 0 ) $v =
'null';
71 "\n\t$aggfn(IIF($colfield=$vq,1,0)) AS \"$v\", " 73 "\n\t$aggfn(CASE WHEN $colfield=$vq THEN 1 ELSE 0 END) AS \"$v\", ";
76 if ($hidecnt) $label = $v;
77 else $label =
"{$v}_$aggfield";
79 "\n\t$aggfn(IIF($colfield=$vq,$aggfield,0)) AS \"$label\", " 81 "\n\t$aggfn(CASE WHEN $colfield=$vq THEN $aggfield ELSE 0 END) AS \"$label\", ";
85 if ($aggfield && $aggfield !=
'1'){
86 $agg =
"$aggfn($aggfield)";
87 $sel .=
"\n\t$agg as \"$sumlabel$aggfield\", ";
91 $sel .=
"\n\tSUM(1) as Total";
93 $sel = substr($sel,0,strlen($sel)-2);
97 $rowfields = preg_replace(
'/ AS (\w+)/i',
'', $rowfields);
99 $sql =
"SELECT $sel \nFROM $tables $where \nGROUP BY $rowfields";
109 # Query the main "product" table 110 # Set the rows to CompanyName and QuantityPerUnit 111 # and the columns to the Categories 112 # and define the joins to link to lookup tables 113 # "categories" and "suppliers" 117 $gDB, # adodb connection
118 'products p ,categories c ,suppliers s', # tables
119 'CompanyName,QuantityPerUnit', # row fields
120 'CategoryName', # column fields
121 'p.CategoryID = c.CategoryID and s.SupplierID= p.SupplierID' # joins/where
147 # Query the main "product" table 148 # Set the rows to CompanyName and QuantityPerUnit 149 # and the columns to the UnitsInStock for diiferent ranges 150 # and define the joins to link to lookup tables 151 # "categories" and "suppliers" 154 $gDB, # adodb connection
155 'products p ,categories c ,suppliers s', # tables
156 'CompanyName,QuantityPerUnit', # row fields
159 ' 0 ' =>
'UnitsInStock <= 0',
160 "1 to 5" =>
'0 < UnitsInStock and UnitsInStock <= 5',
161 "6 to 10" =>
'5 < UnitsInStock and UnitsInStock <= 10',
162 "11 to 15" =>
'10 < UnitsInStock and UnitsInStock <= 15',
163 "16+" =>
'15 < UnitsInStock' 165 ' p.CategoryID = c.CategoryID and s.SupplierID= p.SupplierID', # joins/where
166 'UnitsInStock', # sum
this field
167 'Sum' # sum label prefix
if(isset($_REQUEST['nrows'])) else $rs
PivotTableSQL(&$db, $tables, $rowfields, $colfield, $where=false, $aggfield=false, $sumlabel='Sum ', $aggfn='SUM', $showcount=true)
rs2html(&$rs, $ztabhtml=false, $zheaderarray=false, $htmlspecialchars=true, $echo=true)