Welcome Guest, Not a member yet? Register   Sign In
appending array of data to the message parameter of send_email()
#1

[eluser]Brad K Morse[/eluser]
I have a controller function email

Code:
function email() {
    $this->load->model('data_model');

    if($_POST) {
        $data['volunteers'] = $this->data_model->sendChairVolunteerList();
        // need to append data array from sendChairVolunteerList to message parameter of send_email
        send_email('[email protected]', 'List of Volunteers', $_POST['message']);
        $this->load->view('email-sent-view', $data);
    } else {
        $data['venue_chairs'] = $this->data_model->getVenues();
        $this->load->view('email-view', $data);
    }

}

I have a form that you can select the venue chair you want to send an email to, once you hit send, it sends an email via send_email() SS http://cl.ly/3f1M1z181h2O3x1d322E

It works successfully, once the message is sent, it sends it, then displays the email-sent-view.

I want to include more within the message that gets sent. I need it to include a list of names from the data table, where venue_id equals $_POST['venue_id'], which is value of the drop-down box you see in the screenshot (http://cl.ly/3f1M1z181h2O3x1d322E)

I have the function sendChairVolunteerList() that gets the venue_id that was posted, runs a get('data') active record call, stores into $data array, seen below

Code:
function sendChairVolunteerList() {
    $q = $this->db->where('venue_id',$_POST['venue_id'])->get('data');
    
    if($q->num_rows() > 0) {
        foreach($q->result() as $row) {
            $data[] = $row;
        }
    return $data;
    }
}

I am new to CI, so inserting the data array (list of volunteers "select from data where $_POST['venue_id'] = venue_id") into the message parameter of send_email() is new to me.

Any help is appreciated.
#2

[eluser]zac[/eluser]
You need to build your message separately, then pass it in to send_email(). Something like this:

Code:
if($_POST) {
    $data['volunteers'] = $this->data_model->sendChairVolunteerList();

    $msg = "This is the list of volunteers:\n\n";
    $msg .= implode(", ",$data['volunteers']); // convert array to a comma-separated list

    send_email('[email protected]', 'List of Volunteers', $msg);
    $this->load->view('email-sent-view', $data);
} ...

Or if building your message directly in the function is too awkward, use a view as a template. Setting the third argument to 'true' returns the parsed view instead of rendering it:

Code:
$msg = $this->load->view('email-message',$data,true);

send_email('[email protected]', 'List of Volunteers', $msg);
#3

[eluser]Brad K Morse[/eluser]
It is not printing the list of volunteers into $msg

It gives error: Message: Object of class stdClass could not be converted to string

which is this line
Code:
$msg .= implode(", ", $data['volunteers']); // convert array to a comma-separated list

Printed out the data array for sendChairVolunteerList() in email-sent-view and it prints out the appropriate volunteers.
#4

[eluser]Brad K Morse[/eluser]
I resolved the problem of appending the array of data to the $msg variable by looping thru it

Not sure if this is the best way to make this work, so I am open to suggestions.

Here is the revised email controller function
Code:
function email() {
    $this->load->model('data_model');

    if($_POST) {
        $data['volunteers'] = $this->data_model->sendChairVolunteerList();
    
        $msg = "This is the list of volunteers:\n\n";

        foreach($data['volunteers'] as $r) {
            $msg .= $r->first_name.", ";
        }
        
    send_email('[email protected]', 'List of Volunteers', $msg);

    $this->load->view('email-sent-view', $data);
        
    } else {
        $data['venue_chairs'] = $this->data_model->getVenues();
        $this->load->view('email-view', $data);
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB