Rumah  >  Artikel  >  php教程  >  php导出数据到csv文件

php导出数据到csv文件

WBOY
WBOYasal
2016-05-25 16:42:371572semak imbas

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

<?php
include ("../admin/inc/inc.php");
$times = time();
$filename = $times . ".csv";
$a = "联系人,联系电话,小区,所需材料,备注,申请时间 ";
$days = postget("days");
$mktime = daystomktime($days);
$sql = "select * from  tugou where ($times-times)<$mktime";
$db = new db();
$result = $db->query($sql);
$rs = $db->fetch($result, 0);
foreach ($rs as $v => $vv) {
    $a.= $vv[&#39;name&#39;] . &#39;,&#39; . $vv[&#39;mo&#39;] . "," . $vv[&#39;comm&#39;] . "," . commbox($vv[&#39;cbox&#39;]) . ",";
    $a.= "" . $vv[&#39;bak&#39;] . "," . date(&#39;y-m-d&#39;, $vv[&#39;times&#39;]) . " 
";
}
//echo $a;
$hod = fopen($filename, "w+");
if (fwrite($hod, $a)) {
    echo "生成文件成功,点击<a href=$filename target=_blank>下载excel 
文档</a>";
}
function daystomktime($days) {
    switch ($days) {
        case 1:
            $t = 86400;
            break;
        case 7:
            $t = 86400 * 7;
            break;
        case 30:
            $t = 86400 * 30;
            break;
        case 90:
            $t = 86400 * 90;
            break;
        case 2048:
            $t = 86400 * 2048;
            break;
    }
    return $t;
}
function commbox($ars) {
    if (trim($ars) == &#39;&#39;) {
        return &#39; &#39;;
    }
    $array = include ("tugou.php");
    $tmp = explode(&#39;,&#39;, $ars);
    $str = &#39;&#39;;
    if ($tmp) {
        foreach ($tmp as $c) {
            $str.= $array[intval($c) ] . &#39;|&#39;;
        }
    }
    return $str;
}
?>

数据库的结构代码如下:

create table if not exists `tugou` ( 
  `id` int(8) not null auto_increment, 
  `name` varchar(20) default null comment &#39;名字&#39;, 
  `mo` varchar(20) default null comment &#39;手机&#39;, 
  `comm` varchar(100) default null, 
  `bak` varchar(500) default null comment &#39;备份&#39;, 
  `cbox` varchar(500) default null comment &#39;类型&#39;, 
  `times` int(4) default null, 
  `ip` varchar(20) default null,//开源代码phprm.com 
  primary key  (`id`) 
) engine=innodb  default charset=utf8 auto_increment=8 ;

  


本文地址:

转载随意,但请附上文章地址:-)

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php简单的伪原创程序 Artikel seterusnya:php用户登录实例教程代码