Home about IT Motivation Course Sales Project About Me

Thursday, June 18, 2009

PHP: create pivot table use ADODB

<?

//includekan class addob

include('adodb/adodb.inc.php');
include "adodb/pivottable.inc.php";
$db = ADONewConnection("mysql");
$db->Connect('localhost','id','password','dbname') or die("COULD NOT SELECT DATABASE.<br>");

$sql = PivotTableSQL(
$db, # adodb connection
'berita b, kategori k', # tables
'pc_berita as PC_Name', # rows (multiple fields allowed)
'nama_berita', # column to pivot on
'b.nama_berita = k.nama_kategori and tgl_berita >= "2009-06-01"' # joins/where
);

$db->SetFetchMode(ADODB_FETCH_ASSOC);
//tampilin datanya (buat tabel)
$rs = $db->Execute($sql);
if ($rs) {
echo "<table border ='1'>";
echo "<tr>";
$head = $rs->fields;
foreach($head as $hcol=>$col)
echo "<th>".ucfirst($hcol)."</th>";

echo "</tr>";
while (!$rs->EOF) {
$row = $rs->fields;
echo "<tr>";
$x =0;
foreach($row as $head=>$col)
{

echo "<td>$col</td>";
$x++;

}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";
}
//tampilkan generate sql nya
//echo $sql;

?>

http://phplens.com/adodb/pivot.tables.html

No comments: