2014-11-29 2 views
0

나는 내가 가치와 키된답니다 insert_batch()

의 범위를 가질 수있는 배열을 데이터 $은이

function checkboxes($data,$category) 
    { 

     $insert=array(
         'story'=>$data 
         'category'=>$category 
        ); 

     $this->db->insert_batch('stories_to_categories',$insert); 
    } 

같은 것을 트링하고

을 insert_batch 사용하는 방법을 내려고 노력하고 OK 나는에서 실현하려 노력이

(
    [0] => 1 
    [1] => 6 
    [2] => 14 
    [3] => 15 
    [4] => 18 
) 

범주에 들어 난 예를 들어 하나의 값을가집니다 나의 타블렛

story category 
------------------------ 
    1  2 
    6  2 
    14  2 
    15  2 
    18  2 

누군가 나를 도와 줄 수 있습니까?

+0

['insert' 하나 개의 레코드를 삽입하고 'insert_batch' 여러 레코드를 삽입하기위한 것이다 (HTTPS//ellislab.com/codeigniter/user-guide/database/active_record.html#insert). 그래서 당신이 겪고있는 문제는 무엇입니까? – Sparky

답변

0

코드를 약간 수정하면이 작업을 수행 할 수 있습니다. CI 문서에서는 일괄 삽입에 연관 배열이 포함 된 배열이 필요하다는 것을 보여줍니다. 삽입 될 각 새 행에 대해 연관 배열을 사용하여 열을 값에 매핑합니다.

실제로, 당신은 당신의 $insert이 같은 배열을 구축 할 것입니다 :

$insert=array(
    array('story'=>1, 'category'=>2). 
    array('story'=>6, 'category'=>2). 
    array('story'=>14, 'category'=>2). 
    array('story'=>15, 'category'=>2). 
    array('story'=>18, 'category'=>2). 
); 

당신의 카테고리가 일정하므로,이 기능을 사용할 수 있습니다

function _insert_($data, $category='2', $options=array('data'=>'story', 'category'=>'category')) 
{ 
    $return = array(); 
    foreach ($data as $value) 
    { 
     $return[] = array($options['data']=>$value, $options['category']=>$category); 
    } 
    return $return; 
} 

그런 다음 수를 다음과 같은 것이 있습니다.

$this->db->insert_batch('stories_to_categories',_insert_($data)); 

희망이 있습니다. 여기

참조 CodeIgniter의 참조 : 아래

찾기 참조 (들) CodeIgniter Active Record: #Insert

+0

좋은 하나 내 용액 – user3827056

+0

기능 체크 박스 ($ category_checkboxes, $의 last_story_id) { \t의 foreach ($ $ 상자 등 category_checkboxes)를 참조 \t { $ 삽입 [= 어레이 ( \t \t \t \t "카테고리"=> $ 상자, \t \t \t \t "이야기"=> $ last_story_id \t \t \t \t); } $ this-> db-> insert_batch ('stories_to_categories', $ insert); } – user3827056

+0

스트레이트 포인트. 좋은 분, @ user3827056 – linil

0
function checkboxes($category_checkboxes,$last_story_id) 
    { 
     foreach ($category_checkboxes as $box) 
     { 
      $insert[] = array(
           "category" => $box, 
           "story" => $last_story_id 
          ); 

     } 

     $this->db->insert_batch('stories_to_categories',$insert); 
    } 
관련 문제