首页 > 开发 > PHP > 正文

php导出数据到csv文件

2024-05-04 21:58:30
字体:大 中 小
来源:转载
供稿:网友

PHP导出数据到CSV文件实例代码如下:

  1. include("../admin/inc/inc.php"); 
  2.  $times = time(); 
  3.  $filename = $times.".csv";  
  4.  $a = "联系人,联系电话,小区,所需材料,备注,申请时间 ";  
  5.  $days = postget("days");  
  6.  $mktime = daystomktime($days); 
  7.  $sql = "select * from  tugou where ($times-times)<$mktime"; 
  8.  $db = new db(); 
  9.  $result = $db->query( $sql ); 
  10.  $rs = $db->fetch($result,0); 
  11.    foreach($rs as $v=>$vv) 
  12.    {   
  13.      $a.=$vv['name'].','. $vv['mo'].",". $vv['comm'].",". 
  14. commbox($vv['cbox']).","; 
  15.      $a.="".$vv['bak'].",". date('y-m-d',$vv['times'])." 
  16. ";   
  17.    }  
  18.   //echo $a; 
  19.   $hod = fopen ($filename,"w+"); 
  20.   if( fwrite($hod,$a) )  
  21.   { 
  22.    echo "生成文件成功,点击<a href=$filename target=_blank>下载excel 
  23. 文档</a>"; 
  24.   } 
  25.    
  26.   
  27. function daystomktime($days) 
  28. { 
  29.  switch( $days ) 
  30.  { 
  31.   case 1: 
  32.    $t = 86400; 
  33.    break; 
  34.   case 7: 
  35.    $t = 86400*7; 
  36.    break; 
  37.   case 30: 
  38.    $t = 86400*30; 
  39.    break; 
  40.   case 90: 
  41.    $t = 86400*90; 
  42.    break; 
  43.   case 2048: 
  44.    $t = 86400*2048; 
  45.    break; 
  46.  } 
  47.  return $t; 
  48. } 
  49. function commbox( $ars ) 
  50.  { 
  51.   if( trim( $ars ) == '' ) 
  52.   { 
  53.    return '&nbsp;'; 
  54.   } 
  55.    
  56.   $array = include("tugou.php"); 
  57.   $tmp = explode(',',$ars); 
  58.   $str =''; 
  59.   if( $tmp ) 
  60.   { 
  61.    foreach( $tmp as $c ) 
  62.    { 
  63.    $str .= $array[intval($c)].'|'; 
  64.    } 
  65.   } 
  66.   return $str; 
  67.  } 

数据库的结构代码如下:

  1.  create table if not exists `tugou` ( 
  2.   `id` int(8) not null auto_increment, 
  3.   `name` varchar(20) default null comment '名字', 
  4.   `mo` varchar(20) default null comment '手机', 
  5.   `comm` varchar(100) default null, 
  6.   `bak` varchar(500) default null comment '备份', 
  7.   `cbox` varchar(500) default null comment '类型', 
  8.   `times` int(4) default null, 
  9.   `ip` varchar(20) default null,//开源代码Vevb.com 
  10.   primary key  (`id`) 
  11. ) engine=innodb  default charset=utf8 auto_increment=8 ; 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表