Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter using associative array in Email class

I have a certain associative array which contains a list of emails. It's given below:

Array
(
    [0] => Array
        (
            [email] => [email protected]
        )

    [1] => Array
        (
            [email] => [email protected]
        )

    [2] => Array
        (
            [email] => [email protected]
        )

    [3] => Array
        (
            [email] => [email protected]
        )

)

It's being generated from the following controller code:

$data['list_of_emails'] = $this->Trainee_requisition->get_list_of_emails($id);

And the corresponding model code from where the get_list_of_emails($id) method is being accessed:

function get_list_of_emails($id){
        $q = $this->db->query("SELECT `email` FROM `requisition_emails` WHERE `req_id` = $id")->result_array();
        return $q;
}

In the Codeigniter documentations, I've found a way of passing a single dimensional array in the $this->mail->to() method of the Email class, but there is no example of an associative array.

My question is, how can I use the above mentioned associative array in the $this->email->to() method?

My main objective is to generate something like this:

$email_list = array('[email protected]', '[email protected]', '[email protected]', '[email protected]');
$this->email->to($email_list);

Edit:

Although Hassaan's answer is the easiest solution and meets my need perfectly, I'd still appreciate answers which can provide a way to retrieve only one column from a query which has more than one column.

For example, if my query is modified like this:

$q = $this->db->query("SELECT rem.`email`, sr.`is_approve` AS approval_status FROM `requisition_emails` rem INNER JOIN `staff_requisitions` sr ON sr.`id` = rem.`req_id` WHERE rem.`req_id` = $id")->result_array();

but I want only email column to be used from the list, then what is the best possible way?

Edit - 2:

Forgot to mention. PHP version is 5.3.

like image 815
Choudhury Saadmaan Mahmid Avatar asked Mar 21 '26 07:03

Choudhury Saadmaan Mahmid


2 Answers

You need to use foreach loop in your view.

Try Example

foreach($list_of_emails as $email)
{
    echo $email."<br />";
    // You can call '$this->mail->to()' to mail.
}
like image 187
Hassaan Avatar answered Mar 23 '26 19:03

Hassaan


As per CI documentation, you can send email to multiple recipients using the following construction:

$this->email->to('[email protected], [email protected], [email protected]');

In order to generate this kind of string, do following steps:

Don't touch your model

Use PHP implode and array_column functions to extract email field and to concat all array elements by comma:

$data['list_of_emails']=$this->Trainee_requisition->get_list_of_emails($id);//Get data from model as one dimensional string
$email_list = implode(',',array_column($data['list_of_emails'],'email'));//Implode array elements with comma as separator
$this->email->to($email_list);

You can print $email_list to ensure you have right format by calling var_dump($email_list); exit;.

EDIT (If you are using PHP < 5.5)

Use array_map function, and make the first parameter as anyonimus function. By defining $columnName, you can extract any column you want.

 $columnName = "email";
$result = array_map(
    function ($arrayRow) use ($columnName){
        return $arrayRow[$columnName];
    }
,$data['list_of_emails']);
var_dump(implode(",",$result));
$this->email->to($result );
like image 43
MrD Avatar answered Mar 23 '26 19:03

MrD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!