您的位置:首页 > 编程语言 > PHP开发

php+mysql导出CSV数据文件

2009-03-31 09:18 1016 查看
请求export_csv.php时如果合法用户就会弹出下面这样的保存下载框:



代码如下:

<?php

require_once('main.php');

if($_COOKIE['CRM_UID'] == null){

showmessage("非法用户!","login.php",1250,'icon_error.jpg');

}

if($_COOKIE['CRM_TYPE'] == 4){

showmessage( "您没有权限!", 'index.php?file=online', 1250, 'icon_error.jpg');

}

$type = $_GET['t'];

export_csv($type);

/**

*导出到CSV文件

* @param <type> $type

*/

function export_csv($type)

{

$filename = date('YmdHis').".csv";

header("Content-type:text/csv");

header("Content-Disposition:attachment;filename=".$filename);

header('Cache-Control:must-revalidate,post-check=0,pre-check=0');

header('Expires:0');

header('Pragma:public');

echo array_to_string(get_export_data($type));

}

/**

*导出数据转换

* @param <type> $result

*/

function array_to_string($result)

{

if(empty($result)){

return i("没有符合您要求的数据!^_^");

}

$data;

$size_result = sizeof($result);

for($i = 0 ; $i < $size_result ; ++ $i) {

$data .= i($result[$i]['orderID']).','.i($result[$i]['username'])."\n";

}

return $data;

}

/**

*获取导出报表的数据

* @param <type> $condition

* @return <type>

*/

function get_export_data($condition)

{

$where = " state = 0 ";

switch($condition)

{

case 'week':

$where .= " and week(date) = week(current_date()-1 ) ;";//每周从周一开始

break;

case 'month':

$where .= " and month(date) = month(current_date()) ;";

break;

case 'year':

$where .= " and year(date) = year(current_date()) ;";

break;

default:

break;

}

$sql = " select * from net_order_detail where {$where}";

$db = get_db();

$res = $db->get_results($sql,ARRAY_A);

return $res;

}

/**

*编码转换

* @param <type> $strInput

* @return <type>

*/

function i($strInput)

{

return iconv('utf-8','gb2312',$strInput);

}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: