Skip to content

Instantly share code, notes, and snippets.

@ecogswell
Created October 19, 2012 16:33
Show Gist options
  • Save ecogswell/3919204 to your computer and use it in GitHub Desktop.
Save ecogswell/3919204 to your computer and use it in GitHub Desktop.
public function getCsv()
{
$csv = '';
$this->_isExport = true;
$this->_prepareGrid();
$sql = (string)$this->getCollection()->getSelect()->__toString();
$db = Mage::getResourceSingleton('core/resource')->getReadConnection();
$result = $db->query($sql);
$per_page = 100;
$pages = ceil($result->rowCount()/$per_page);
$data = array();
foreach ($this->_columns as $column) {
if (!$column->getIsSystem()) {
$data[] = '"'.$column->getExportHeader().'"';
}
}
$csv.= implode(',', $data)."\n";
for ($page=1; $page<=$pages; $page++) {
$this->getCollection()->setPage($page,$per_page);
$this->getCollection()->load();
$this->_afterLoadCollection();
foreach($this->getCollection() as $item) {
$data = array();
foreach ($this->_columns as $column) {
if (!$column->getIsSystem()) {
$data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
$column->getRowFieldExport($item)) . '"';
}
}
$csv.= implode(',', $data)."\n";
}
}
if ($this->getCountTotals())
{
$data = array();
foreach ($this->_columns as $column) {
if (!$column->getIsSystem()) {
$data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
$column->getRowFieldExport($this->getTotals())) . '"';
}
}
$csv.= implode(',', $data)."\n";
}
return $csv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment