write('php://output', $rows);
}
/**
* Define column mapping between CSV and SQL columns
*
* @access public
* @param array $columns
* @return Csv
*/
public function setColumnMapping(array $columns)
{
$this->columns = $columns;
return $this;
}
/**
* Write HTML file
*
* @access public
* @param string $filename
* @param array $rows
* @return Html
*/
public function write($filename, array $rows)
{
$fp = fopen($filename, 'w');
if (is_resource($fp)) {
$types = array_shift($rows);
$this->writeHeader($fp);
foreach ($rows as $row) {
$this->writeRow($fp, $types, $row);
}
$this->writeFooter($fp);
fclose($fp);
}
return $this;
}
/**
* write a HTML header
*
* @param $fp filepointer
*/
private function writeHeader($fp)
{
fwrite($fp,"
\n");
fwrite($fp,"");
}
/**
* write a single row
*
* @param fp filepointer
* @param $row row
*/
private function writeRow($fp, array $types, array $row)
{
fwrite($fp,"");
foreach ($row as $key => $value) {
fwrite($fp,"".$value." | ");
}
fwrite($fp,"
\n");
}
/**
* write a HTML footer
*/
private function writeFooter($fp)
{
fwrite($fp,"
");
}
/**
* Associate columns header with row values
*
* @access private
* @param array $row
* @return array
*/
private function associateColumns(array $row)
{
$line = array();
$index = 0;
foreach ($this->columns as $sql_name => $csv_name) {
if (isset($row[$index])) {
$line[$sql_name] = $row[$index];
} else {
$line[$sql_name] = '';
}
$index++;
}
return $line;
}
/**
* Filter empty rows
*
* @access private
* @param array $row
* @return array
*/
private function filterRow(array $row)
{
return array_filter($row);
}
}