0

I have a form for a hotel site, where I want to update its services, and the client wants to update multiple services at a time. However I'm lost as how to save it in the database with the model.

I already built my controller, it looks something like this:

$items = array(
        array(
                'id'           => $this->input->post('id1', true),
                'hotel_id'     => $hotel_id,
                'des_servicio' => $this->input->post('des_servicio1', true),
                'est_activo'   => $this->input->post('est_activo1', true)
        ),
        array(
                'id'           => $this->input->post('id2', true),
                'hotel_id'     => $hotel_id,
                'des_servicio' => $this->input->post('des_servicio2', true),
                'est_activo'   => $this->input->post('est_activo2', true)
        ),
        array(
                'id'           => $this->input->post('id3', true),
                'hotel_id'     => $hotel_id,
                'des_servicio' => $this->input->post('des_servicio3', true),
                'est_activo'   => $this->input->post('est_activo3', true)
        )
);

$this->hotel_model->save_multiple($items);

[UPDATE]

this is how my new model looks like:

function save_multiple($items = array())
{
    $this->db->insert_batch($this->__tabla, $items);
    return $this->db->affected_rows();
}

My issue is that now it creates 10 rows (my original form has 10 fields) even if I only populate 3 fields. So in my database 3 services get stored, and also 7 blank rows. How can change this?

2
  • You might also want to look at $this->db->insert_batch('mytable', $items); Commented Mar 21, 2013 at 16:24
  • @Aaron thank you that's very helpful, but now I have the problem I mention, the fields are not optional and it creates empty rows when I don't populate them. As I mention my original form has 10 fields, so when I only fill 2 or 3, 10 rows are still created at the database. Do you know how to change this? Commented Mar 21, 2013 at 16:35

2 Answers 2

1
foreach $items //I get an error here
    as $item 

should be

foreach ( $items as $item ) 
Sign up to request clarification or add additional context in comments.

1 Comment

oh that's just sintax error i made at the moment of typing, I'm sorry my original code does have the parenthesis. I'll try right now what you said with the insert_batch.
0

Remember that input->post() returns false if the value is not set. So check to see if the value is set be for putting it in the array. Then when the model receives it. It saves them all. Or the other option is to create a new array in the model from the array that is passed in and then pass the new array to the insert_batch() function.

$items = array();

$id = $this->input->post('id1', true);
if( $id != false ) {
    $items[] = array(
        'id'           => $id, true),
        'hotel_id'     => $hotel_id,
        'des_servicio' => $this->input->post('des_servicio1', true),
        'est_activo'   => $this->input->post('est_activo1', true)
    );
}

$id = $this->input->post('id2', true);
if( $id != false ) {
    $items[] = array(
        'id'           => $id, true),
        'hotel_id'     => $hotel_id,
        'des_servicio' => $this->input->post('des_servicio2', true),
        'est_activo'   => $this->input->post('est_activo2', true)
    );
}
....

1 Comment

thank you! I did something similar to this and then use array_filter() to drop empty arrays so I could pass it to insert_batch() Again thanks for your time :)

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.