通常我在控制器中创建一个新动作来下载 CSV,例如
public function csvAction() {
$year = $this->_getParam('year');
$month = $this->_getParam('month');
$week = $this->_getParam('week');
$arrCsvOutputCols = array();
$title = '';
if ($year && $month && $week) {
$models = $this->db()->fetchAll("
SELECT
YEAR(`create_date`) AS `year`,
MONTH(`create_date`) AS `month`,
WEEK(`create_date`) AS `week`,
DAY(`create_date`) AS `day`,
SUM(case WHEN `age` < 21 then 1 else 0 end) `a`,
SUM(case WHEN `age` >= 21 AND `age` < 31 then 1 else 0 end) `b`,
SUM(case WHEN `age` >= 31 AND `age` < 41 then 1 else 0 end) `c`,
SUM(case WHEN `age` >= 41 AND `age` < 51 then 1 else 0 end) `d`,
SUM(case WHEN `age` >= 51 AND `age` < 61 then 1 else 0 end) `e`,
SUM(case WHEN `age` >= 61 AND `age` < 71 then 1 else 0 end) `f`,
SUM(case WHEN `age` >= 71 AND `age` < 71 then 1 else 0 end) `g`,
SUM(case WHEN `age` > 80 then 1 else 0 end) `h`
FROM
dtb_member
GROUP BY `year`, `month`, `week`,`day` HAVING `year` = ? AND `month` = ? AND `week` = ?
ORDER BY `day` ASC;"
, array($year, $month, $week));
array_push($arrCsvOutputCols,'year','month','week','day');
$title = '"Year","Month","Week","Day",';
} elseif ($year && $month) {
$models = $this->db()->fetchAll("
SELECT
YEAR(`create_date`) AS `year`,
MONTH(`create_date`) AS `month`,
WEEK(`create_date`) AS `week`,
SUM(case WHEN `age` < 21 then 1 else 0 end) `a`,
SUM(case WHEN `age` >= 21 AND `age` < 31 then 1 else 0 end) `b`,
SUM(case WHEN `age` >= 31 AND `age` < 41 then 1 else 0 end) `c`,
SUM(case WHEN `age` >= 41 AND `age` < 51 then 1 else 0 end) `d`,
SUM(case WHEN `age` >= 51 AND `age` < 61 then 1 else 0 end) `e`,
SUM(case WHEN `age` >= 61 AND `age` < 71 then 1 else 0 end) `f`,
SUM(case WHEN `age` >= 71 AND `age` < 71 then 1 else 0 end) `g`,
SUM(case WHEN `age` > 80 then 1 else 0 end) `h`
FROM
dtb_member
GROUP BY `year`, `month`, `week` HAVING `year` = ? AND `month` = ?
ORDER BY `week` ASC;"
, array($year, $month));
array_push($arrCsvOutputCols,'year','month','week');
$title = '"Year","Month","Week",';
} elseif ($year) {
$models = $blood = $this->db()->fetchAll("
SELECT
YEAR(`create_date`) AS `year`,
MONTH(`create_date`) AS `month`,
SUM(case WHEN `age` < 21 then 1 else 0 end) `a`,
SUM(case WHEN `age` >= 21 AND `age` < 31 then 1 else 0 end) `b`,
SUM(case WHEN `age` >= 31 AND `age` < 41 then 1 else 0 end) `c`,
SUM(case WHEN `age` >= 41 AND `age` < 51 then 1 else 0 end) `d`,
SUM(case WHEN `age` >= 51 AND `age` < 61 then 1 else 0 end) `e`,
SUM(case WHEN `age` >= 61 AND `age` < 71 then 1 else 0 end) `f`,
SUM(case WHEN `age` >= 71 AND `age` < 71 then 1 else 0 end) `g`,
SUM(case WHEN `age` > 80 then 1 else 0 end) `h`
FROM
dtb_member
GROUP BY `year`, `month` HAVING `year` = ?
ORDER BY `month` ASC;"
, $year);
array_push($arrCsvOutputCols,'year','month');
$title = '"Year","Month",';
} else {
$models = $this->db()->fetchAll("
SELECT
YEAR(`create_date`) AS `year`,
SUM(case WHEN `age` < 21 then 1 else 0 end) `a`,
SUM(case WHEN `age` >= 21 AND `age` < 31 then 1 else 0 end) `b`,
SUM(case WHEN `age` >= 31 AND `age` < 41 then 1 else 0 end) `c`,
SUM(case WHEN `age` >= 41 AND `age` < 51 then 1 else 0 end) `d`,
SUM(case WHEN `age` >= 51 AND `age` < 61 then 1 else 0 end) `e`,
SUM(case WHEN `age` >= 61 AND `age` < 71 then 1 else 0 end) `f`,
SUM(case WHEN `age` >= 71 AND `age` < 71 then 1 else 0 end) `g`,
SUM(case WHEN `age` > 80 then 1 else 0 end) `h`
FROM
dtb_member
GROUP BY `year` HAVING `year` > 0
ORDER BY `year` ASC;"
);
array_push($arrCsvOutputCols,'year');
$title = '"Year",';
}
ini_set('memory_limit','-1');
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
header('Content-type: application/octet-stream');
if (preg_match("/MSIE 8\.0/", $_SERVER['HTTP_USER_AGENT'])) {
header('Content-Disposition: filename=age_' . time() . '.csv');
} else {
header('Content-Disposition: attachment; filename=age_' . time() . '.csv');
}
header('Pragma: public');
header('Cache-control: public');
array_push($arrCsvOutputCols,'a','b','c','d','e','f','g');
$arrCsvOutputTitle = $title.'"0-20","21-30","31-40","41-50","51-60","61-70","71-80","81-"';
echo mb_convert_encoding($arrCsvOutputTitle, 'SJIS-win', 'UTF-8') . "\r\n";
foreach ($models as $model) {
$item = $model;
$cols = array();
foreach ($arrCsvOutputCols as $col) {
if ($col) {
$value = $item[$col];
$value = str_replace("\r", "", $value);
$value = str_replace("\n", "", $value);
$value = str_replace("\"", "\"\"", $value);
array_push($cols, '"' . $value . '"');
} else {
array_push($cols, '');
}
}
echo mb_convert_encoding(join(",", $cols), 'SJIS-win', 'UTF-8') . "\r\n";
}
}