protected function getPivotData() { $recordset = parent::fetchDataSet(); // convert the normal record set to pivot data array $data = Pivot::factory($recordset)->pivotOn(array_keys($this->pivotConfig['rows']))->addColumn(array_keys($this->pivotConfig['columns']), array_keys($this->pivotConfig['datas']))->fullTotal()->pivotTotal()->fetch(1); //print_r($data); exit; return $data; }
echo "<thead>"; foreach (array_keys($data[0]) as $item) { echo "<td><b>{$item}<b></td>"; } echo "</thead>"; foreach ($data as $row) { echo "<tr>"; foreach ($row as $item) { echo "<td>{$item}</td>"; } echo "</tr>"; } echo "</table>"; } echo "<h2>original data</h2>"; simpleHtmlTable($recordset); echo "<h2>pivot on 'host'</h2>"; $data = Pivot::factory($recordset)->pivotOn(array('host'))->addColumn(array('year', 'month'), array('users', 'clicks'))->fetch(); simpleHtmlTable($data); echo "<h2>pivot on 'host' with totals</h2>"; $data = Pivot::factory($recordset)->pivotOn(array('host'))->addColumn(array('year', 'month'), array('users', 'clicks'))->fullTotal()->lineTotal()->fetch(); simpleHtmlTable($data); echo "<h2>pivot on 'host' and 'country'</h2>"; $data = Pivot::factory($recordset)->pivotOn(array('host', 'country'))->addColumn(array('year', 'month'), array('users', 'clicks'))->fullTotal()->pivotTotal()->lineTotal()->fetch(); simpleHtmlTable($data); echo "<h2>pivot on 'host' and 'country' with group count</h2>"; $data = Pivot::factory($recordset)->pivotOn(array('host', 'country'))->addColumn(array('year', 'month'), array('users', 'clicks', Pivot::count('count')))->fullTotal()->pivotTotal()->lineTotal()->fetch(); simpleHtmlTable($data); echo "<h2>pivot on 'country' with group count</h2>"; $data = Pivot::factory($recordset)->pivotOn(array('host'))->addColumn(array('country'), array('year', Pivot::count('count')))->lineTotal()->fullTotal()->fetch(); simpleHtmlTable($data);
<link href="../../css/normalize.css" rel="stylesheet" media="all"> <link href="../../css/styles.css" rel="stylesheet" media="all"> </head> <body> <header id="header" role="banner"> </header> <div id="content" class="wrap"> <main role="main"> <section> <article id="main_article"> <?php $headers = array('id', 'userid', 'department', 'courseid', 'fullname', 'category', 'category name', 'timeenrolled', 'timestarted', 'timecompleted'); echo "<h2>original data</h2>"; simpleHtmlTable($recordset, $headers); echo "<h2>pivot on userid</h2>"; $data = Pivot::factory($recordset)->pivotOn(array('userid'))->addColumn(array('category name'), array('timecompleted'))->fetch(); if (empty($data)) { echo "Error: Pivot table not returned!"; } else { simpleHtmlTable($data, array('userid', 'fullname', 'department', 'timecompleted')); } ?> </article> </section> </main> </div> <footer role="contentinfo"> <?php echo $OUTPUT->footer(); ?> </footer>