Matt Saunders
Matt Saunders

Reputation: 4371

Codeigniter array issue

I'm trying to loop through an array and insert my values into the database. My table looks like this:

My form enables the user to select a day, an opening time and a closing time. There is a plus button which adds another day below it, so the user can choose whichever days they wish. This means that my select box names are arrays, i.e. hours_day[]

My Model looks like this:

$hours = array(
    'hours_day' => $this->input->post('venue_hours_day'),
    'hours_opening' => $this->input->post('venue_hours_open'),
    'hours_closing' => $this->input->post('venue_hours_close'),
);

So I have an array ($hours) with arrays inside of it (hours_day, hours_opening, hours_closing). How do I loop through this an add it to my database?

Upvotes: 1

Views: 34

Answers (1)

Vincent Decaux
Vincent Decaux

Reputation: 10714

You can use :

$post_day = $this->input->post('venue_hours_day');
$post_opening = $this->input->post('venue_hours_open');
$post_closing = $this->input->post('venue_hours_closing');

$count = count($post_day);
$results = array();

for ($i = 0; $i < $count; $i++)
{
    $results []= array(
      'hours_day' => $post_day[$i],
      'hours_opening' => $post_opening[$i],
      'hours_closing' => $post_closing[$i]
    );
}

$this->db->insert_batch('your_table', $results);

Upvotes: 1

Related Questions