duanjiu3486 2015-09-11 15:39
浏览 34

具有fputcsv和循环功能的CSV生成器

How should I use tabulate?

$naglowek = "ID,\t Imię,\t Nazwisko,\t Data urodzenia,\t Miasto,\t EMAIL";
$naglowek = explode(";",$naglowek); // header
fputcsv($fp, $naglowek); 
while($r = $result->fetch_assoc()){
    $ciag = '';
    $ciag =  $r['id'].','.$r['name'].','.$r['surName'].','.$r['dateOfBirth'].','.$r['city'].','.$r['email'];
    $ciag = explode(";",$ciag);
    fputcsv($fp, $ciag,"\t");
}

I added "\t" but it isn't working.

Below is question, what somebody answered.
I made a CSV generator for my client. It should get data from DB (mysqli). The code is :

while($r = $result->fetch_assoc()){
  $someVariable = $r['id'].','.$r['name'].','.$r['surName'].','.$r['dateOfBirth'].','.
  $r['city'].','.$r['email'];
}

It is working. But I go to next step to use a function fputcsv. It is from PHP5. This is how my DB looks: My DB In PHPManual fputcsv is using like:

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

How can I use my data to this array ? My data will be from DataBase and will be generated dynamically by the loop above.

  • 写回答

1条回答 默认 最新

  • doujiao7483 2015-09-11 15:55
    关注

    You can do that in this way:

    $fp = fopen('out.csv', 'w');
    
    while($r = $result->fetch_assoc()){
        fputcsv($fp, $r);
    }
    
    fclose($fp);
    

    For each line that fetch from the database, it will include in out.csv file.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP