2014-04-22 2 views
1

배열을 반복하고 데이터베이스에 값을 삽입하려고합니다. 내 표는 다음과 같습니다코드 서명자 배열 문제

  • 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

당신은 단지이 $ this-> DB-> ('TABLENAME', $ 시간)를 삽입; 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() 함수가 있다는 것을 몰랐습니다! –

관련 문제