php csv 导出

header(‘Cache-Control:must-revalidate,post-check=0,pre-check=0‘);
header(‘Expires:0‘);
header(‘Pragma:public‘);
echo array_to_string(get_export_data());
}

function i($strInput) {
return iconv(‘utf-8‘, ‘gb2312‘, $strInput);//页面编码为utf-8时使用,否则导出的中文为乱码
}

function array_to_string($result) {
if (empty($result)) {
return i("没有符合您要求的数据!");
}

//表头
$data = iconv(‘utf-8‘, ‘gb2312‘, "kh_name,kh_address") . "\r\n";

$size_result = sizeof($result);

for ($i = 0; $i $size_result; $i++) {
$data .= i($result[$i][‘kh_name‘]) . ‘,‘ . i($result[$i][‘kh_address‘]) . "\n";
}
return $data;
}

function get_export_data() {

$sql = "SELECT kh_name,kh_address FROM bs_kehu";

global $mysqli;
$result = $mysqli-query($sql);

$res = array();
$i = 0;
while ($row = $result-fetch_array(MYSQLI_ASSOC)) {
$res[$i][‘kh_name‘] = $row[‘kh_name‘];
$res[$i][‘kh_address‘] = $row[‘kh_address‘];
$i++;
}
return $res;
}

php csv 导出

原文地址:http://www.cnblogs.com/meroselove/p/7290225.html


最新回复(0)
/jishuo88HOhUuI5OtvqV_2Fh2okDECbfq1TsIPtr3UmRgNPxPI_3D4719096
8 简首页