将输入写入CSV(Write Inputs to CSV)

编程入门 行业动态 更新时间:2024-10-26 23:33:48
将输入写入CSV(Write Inputs to CSV)

我是PHP新手,无法弄清楚这一点。

我有2个输入(姓名和电子邮件)。 我需要将其写入CSV文件并单击下载。 我发现我需要fputcsv()函数并形成数组数组......

但不知怎的,我不知道如何正确地形成这个数组......有人可以给我一个提示吗?

我已经知道了txt文件......但是根本无法将其转换为csv ...

public function invokeAction(CakeRequest $request){ if ($this->request->is('post')) { if(isset($_POST['subName']) && isset($_POST['subEmail'])) { $data = $_POST['subName'] . '-' . $_POST['subEmail'] . "\n"; $ret = file_put_contents('../tmp/subs.txt', $data, FILE_APPEND | LOCK_EX); unset($_POST['subName']); unset($_POST['subEmail']); } }

Im new to PHP and cant figure this out.

I have 2 inputs(Name and email). I need to write it to CSV file and download on click. I found out that i need fputcsv() function and to form array of arrays...

But somehow i cant see how to form that array correctly... Can someone give me a hint?

Im already wirting to txt file... but simply cant transform it to csv...

public function invokeAction(CakeRequest $request){ if ($this->request->is('post')) { if(isset($_POST['subName']) && isset($_POST['subEmail'])) { $data = $_POST['subName'] . '-' . $_POST['subEmail'] . "\n"; $ret = file_put_contents('../tmp/subs.txt', $data, FILE_APPEND | LOCK_EX); unset($_POST['subName']); unset($_POST['subEmail']); } }

最满意答案

您需要做的唯一事情是用“,”替换subName和subEmail之间的短划线,并将您的文件重命名为subs.csv。

public function invokeAction(CakeRequest $request){ if ($this->request->is('post')) { if(isset($_POST['subName']) && isset($_POST['subEmail'])) { $data = $_POST['subName'] . ',' . $_POST['subEmail'] . "\n"; $ret = file_put_contents('../tmp/subs.csv', $data, FILE_APPEND | LOCK_EX); unset($_POST['subName']); unset($_POST['subEmail']); } }

CSV不是特殊的文件格式。 它只是具有特定格式的纯文本。

The only thing you need to do is replace the dash between subName and subEmail with a "," and rename your file to subs.csv.

public function invokeAction(CakeRequest $request){ if ($this->request->is('post')) { if(isset($_POST['subName']) && isset($_POST['subEmail'])) { $data = $_POST['subName'] . ',' . $_POST['subEmail'] . "\n"; $ret = file_put_contents('../tmp/subs.csv', $data, FILE_APPEND | LOCK_EX); unset($_POST['subName']); unset($_POST['subEmail']); } }

CSV is not a special file format. It's just plain text with a certain format.

更多推荐

本文发布于:2023-08-06 09:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446722.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CSV   Write   Inputs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!