0

I'm trying to enter 100 email addresses separated by ';' and store them in MySql table. What I've tried so far is:

    $recipient_raw = $this->input->post('recipient'); //get the 100 emails in $recipient_raw
    $recipient_array=explode(';', $recipient_raw);  //explode them into an array
    $title = $this->input->post('title');
    $body = $this->input->post('body');

    foreach($recipient_array->result() as $row): 
    $recipient=array(
    'email'=>$row->email             //looping through each email; seems I should not use $row->email since there's no title for them
    );

    $this->db->insert('eamil_send',$this->db->escape($recipient));
    endforeach;

I'm running this on CodeIgniter PHP. Error msg is on line foreach($recipient_array->result() as $row):, where it says: Call to a member function result() on a non-object

Any advice is appreciated! Thanks!

3 Answers 3

1

result(), is an active record function for converting a database result object. You are creating a standard array.

$batch = array();
foreach($recipient as $row){
    $batch[] = array(
        'email' => $row
    );
}
$this->db->insert_batch('email_send', $batch);

That should do what you're trying to accomplish.

Sign up to request clarification or add additional context in comments.

Comments

1

The error message is pretty much self explanatory..

  • In the 2nd line of your code you declared $recipient_array as an Array(), not an object. So, there's no "result" method available. Your loop should be

    foreach($recipient_array as $row)

  • On a side note, you probably shouldn't be executing db operations inside a loop (especially, for 100 operations!). Instead, you should save all the queries in one big query string and execute at the end.

Comments

0

If you want your 100 email address be separate by ',' you should use implode(glue, pieces) instead of explode() function. Like on email address that you want to insert.

$recipient_raw = array();
$recipient_raw[] = $this->input->post('recipient');

foreach ($recipient_raw as $value) {

        $tem_arr = implode(',', $value);
    }

$this->db->insert('eamil_send',$this->db->escape($tem_arr));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.