dujmovicv
dujmovicv

Reputation: 65

CodeIgniter db->insert through loop

I'm new to CI. I want to add some records to the DB with a loop. What I have in my controller is:

$client_name = $this->input->post('client_name');
            $contact_person_details = array($this->input->post('contact_person_name_1'),
                                          $this->input->post('contact_person_phone_1'),
                                          $this->input->post('contact_person_email_1'),
                                          $this->input->post('contact_person_name_2'),
                                          $this->input->post('contact_person_phone_2'),
                                          $this->input->post('contact_person_email_2'),
                                          $this->input->post('contact_person_name_3'),
                                          $this->input->post('contact_person_phone_3'),
                                          $this->input->post('contact_person_email_3'),
                                        );
            $this->common_model->dbinsert_contactpersons('ci_contact_persons', $contact_person_details, $client_name);

Then in my model:

function dbinsert_contactpersons ($tablename, $details, $client_name) {
    for ($i=0; $i<=3; $i++) {
        $w = 3*$i;
        $j = $w+1;
        $k = $w+2;
        $insert_detail = array('client'             => $client_name,
                            'contact_person_name'   => $details[$w],
                            'contact_person_phone'  => $details[$j],
                            'contact_person_email'  => $details[$k]
                            );
        if($this->db->insert ($tablename, $insert_detail))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

The code inserts the FIRST contact person details into the DB (contact_person_name_1, contact_person_phone_1, contact_person_email_1) but does not insert the rest... When I var_dump the array $contact_person_details, the values are there. Thanks for the suggestions...

Begin edit > I was trying to optimize the controller and to get rid of the array if there's an empty value like this:

$contacts_data = array();
            for ($i=1; $i<=3; $i++) {
                if ($this->input->post('contact_person_name_'.$i) != "") {
                    $contacts_data['client'] = $this->input->post('client_name');
                    $contacts_data['contact_person_name'] = $this->input->post('contact_person_name_'.$i);
                    $contacts_data['contact_person_phone'] = $this->input->post('contact_person_phone_'.$i);
                    $contacts_data['contact_person_email'] = $this->input->post('contact_person_email_'.$i);
                }
            }

Obviously, with errors... Is there a correct way to do this?

Upvotes: 0

Views: 1363

Answers (1)

AdrienXL
AdrienXL

Reputation: 3008

You can send an array to the model and do multiple insert in one line:

Controller:

$data = array(
   array(
      'client' => $this->input->post('client_name') ,
      'contact_person_name' => $this->input->post('contact_person_name_1') ,
      'contact_person_phone' => $this->input->post('contact_person_phone_1'),
      'contact_person_email' => $this->input->post('contact_person_email_1'),
   ),
   array(
     'client' => $this->input->post('client_name') ,
      'contact_person_name' => $this->input->post('contact_person_name_2') ,
      'contact_person_phone' => $this->input->post('contact_person_phone_2'),
      'contact_person_email' => $this->input->post('contact_person_email_2'),
   )
);
 $this->common_model->dbinsert_contactpersons('ci_contact_persons', $data);

Model :

function dbinsert_contactpersons ($tablename, $data) 
{
     $this->db->insert_batch($tablename, $data);
}

You can probably optimize the controller part with a loop and arrays (e.g. contact_person_name[]).

More details : http://www.codeigniter.com/user_guide/database/active_record.html#insert

Upvotes: 1

Related Questions