2014-04-22 96 views
1

我试图循环访问一个数组并将我的值插入到数据库中。我的表看起来像这样:Codeigniter阵列问题

  • hours_day
  • hours_open
  • hours_close

我的形式使用户可以选择某一天,一开场时间和结束时间。有一个加号按钮,在它下面增加一天,所以用户可以选择他们希望的任何日子。这意味着,我的选择框中的名称是数组,即hours_day []

我的模型看起来是这样的:

$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'), 
); 

所以我有它的内部数组的数组($小时)(hours_day,hours_opening, hours_closing)。我如何循环通过这个添加到我的数据库?

+0

你只是这个$这个 - > DB->插入( '表名',$小时); bt记住数组键与您的表列相同 –

+0

这将无法正常工作 - 我在数组中有一个数组。 –

回答

1

您可以使用:

$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); 
+0

谢谢!我不知道有一个insert_batch()函数! –