/**
* Download CSV
* @param unknown $orders _id
* @param unknown $orders _date_start
* @param unknown $orders _date_end
*/
Public Function Csvdown ($orders _id, $orders _date_start, $orders _date_end, $title = ' orderssales ') {
Output Excel file header, you can replace user.csv with the file name you want
Header (' Content-type:application/vnd.ms-excel ');
Header (' Content-disposition:attachment;filename= '. $title. '. CSV "');
Header (' cache-control:max-age=0 ');
Open PHP file handle, php://output direct output to browser
$fp = fopen (' php://output ', ' a ');
Generate a file on the server
/* $title = $title. ". csv";
$filePath = App_path. ' /runtime/temp/'. $title;
$fp = fopen ($filePath, ' a '); */
Output Excel Column name information
$head = Array (' 12 ', ' 34 ', ' Platform ', ' shop ', ' Order Date ', ' Customer ID ', ' Sales ', ' cost ', ' freight (including processing fee ') ', ' Fees & charges ', ' profit ', ' profit Margin ');
foreach ($head as $i = = $v) {
CSV Excel support GBK encoding, be sure to convert otherwise garbled
$head [$i] = iconv (' utf-8 ', ' GBK ', $v);
}
Writes data to a file handle via Fputcsv
Fputcsv ($fp, $head);
Download the content
$saleForms = new \erp\c\orders\saleforms ();
Get the total number of orders
$total = $saleForms->getorderssaletotal ($orders _id, $orders _date_start, $orders _date_end);
$num = 5000;
$count = Ceil ($total/$num);
Counter
$cnt = 0;
Every $limit line, refresh the output buffer, not too big, not too small
$limit = 5000;
for ($i =1; $i <= $count; $i + +) {
$start = ($i-1) * $NUM;
$returnInfo = $saleForms->orderssalerun ($orders _id, $orders _date_start, $orders _date_end, $start, $num);
foreach ($returnInfo [' Items '] as $row) {
$cnt + +;
if ($limit = = $cnt) {//Refresh output buffer to prevent problems caused by excessive data
Ob_flush ();
Flush ();
$cnt = 0;
}
CSV Excel support GBK encoding, be sure to convert otherwise garbled
$temp = Array ();
foreach ($row as $v) {
$temp [] = Iconv (' utf-8 ', ' GBK ', $v);
}
Fputcsv ($fp, $temp);
Unset ($temp);
}
};
Fclose ($FP);
}
If the data is very large
Can change the running memory and PHP request run time to resolve
PHP CSV Export