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

时间:2015-05-07 19:14:48

标签: php codeigniter 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函数返回如下数组:

 return $bezoekers->result_array();

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

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

1 个答案:

答案 0 :(得分:1)

尝试更改

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 documentationPHP fopen Documentation - 模式部分。

如果没有这种模式的改变,你基本上就是在每次迭代时从头开始编写文件而不是附加到它。