0

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?

1 Answer 1

1

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

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

3 Comments

Please have a look at my edited 1st question regarding the optimization and dropping 'subarray' if there's a NULL value. Any suggestion is appreciated.
@dujmovicv if ($this->input->post('contact_person_name_'.$i)) should be enough
thanks. it is OK now. I forgot my array was 2d : $contacts_data[$i]['contact_person_name'] instead of $contacts_data['contact_person_name'] inside the loop.

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.