使用 codeigniter 编写电子邮件列表(txt 文件)以导入 mailchimp

Writing an email list (txt file) with codeigniter to import in mailchimp

我想从我的数据库中的所有用户生成一个电子邮件列表。

我遵循了这个指南http://www.codeigniter.com/userguide2/helpers/file_helper.html

我也试过这个:PHP Write string to new line for the first time and not at the end of file

我已经从我的数据库中获得了用户,这是我控制器中的用户:

public function maak_lijst_bezoekers(){

    $this->load->helper('file');
    $this->load->model("Lijst_bezoekers_model");
    $data = $this->Lijst_bezoekers_model->get_all_bezoekers();

    foreach($data as $d){
        if ( ! write_file('./uploads/lijst_bezoekers_mails/lijst_mails.txt', $d['naam']." ".$d['voornaam']." ".$d['email']."\r\n"))
        {
            echo 'Unable to write the file';
        }
        else
        {
            echo 'File written!';
        }
    }

}

get_all_bezoekers 函数 returns 一个这样的数组:

 return $bezoekers->result_array();

我在这个数据库中有 2 个用户,但文本文件中只有一行和一个用户。

有人可以给我一些关于如何使用 codeigniter 将整个列表放入 txt 文件的提示,这样我就可以将它导入 mailchimp 吗?

尝试改变

if ( ! write_file('./uploads/lijst_bezoekers_mails/lijst_mails.txt', $d['naam']." ".$d['voornaam']." ".$d['email']."\r\n"))

if ( ! write_file("./uploads/lijst_bezoekers_mails/lijst_mails.txt", $d['naam']." ".$d['voornaam']." ".$d['email']."\r\n", "a+"))

注意 "a+" 作为 write_file 函数的最后一个参数。请参阅 Codeigniter documentation and PHP fopen Documentation - 模式部分。

如果不更改模式,您基本上是在每次迭代时从头开始编写文件,而不是附加到文件。