fputcsv不会在CSV文件中写入任何数据 [英] fputcsv doesn't write any data in a CSV file

查看:458
本文介绍了fputcsv不会在CSV文件中写入任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站中,我从mysql数据创建一个表,然后现在我想添加一个表的导出按钮buttom,以便用户能够将数据下载为CSV文件。

In my web site I'm creating a table from the mysql data and then now I want to add a export button buttom of the table so that a user will be able to download the data as an CSV file.

为了这样做,我写了虚拟形式:

To do that I wrote dummy form:

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
    <input type="submit" name="submit" value="Click Me">
</form>

在php文件的顶部,我有:

And at the top of the php file I have:

if(isset($_POST['submit'])) {
    export();
}



在我的导出函数中,我有一些mysql的东西,然后:

In my export function I have some mysql stuff, I'm creating an array and pushing data into it, and then:

$header = array('name', 'date', 'total', 'success', 'opens', 'clicks', 'success_rate', 'open_rate', 'CTO', 'CTR')
$fp = fopen('exportme2.csv', 'w');
fputcsv($fp, $header);

foreach ($data as $lines) {
        fputcsv($fp, $lines);
}
fclose($fp);

点击导出按钮后,我会有exportme2.csv文件,
数据可能是错误的,所以除了至少我应该有标题名称。

After I click to export button, I'll have exportme2.csv file, however it is empty ! The data may be wrong so that there won't be anything but at least I should have the header names.

你能帮我解决这个问题吗? ?

Can you help me about this issue please ?

感谢。

推荐答案

它提交页面,以便当页面刷新时,我的所有数组都变为空。

It wasn't posting anything because it submit the page so that when the page refreshed all of my arrays are become empty.

这篇关于fputcsv不会在CSV文件中写入任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆