2013-05-18 2 views
0

여러 텍스트 영역의 값을 가져 와서 데이터베이스에 저장하고 싶습니다. 지금은 서로 다른 값을 가진 각각의 네 가지를 가지고 있고이 값 저장 배치하려는 경우, 텍스트 영역 필드는 다음과 같습니다CodeIgniter에서 여러 HTML 텍스트 영역 저장하기

<textarea name="compedia[]"></textarea> 
<textarea name="specification[]"></textarea> 

및 저장 기능 :

function saveCOA(){ 
    $labref=$this->uri->segment(3); 
    $data= $this->input->post('compedia'); 
    $data1= $this->input->post('specification'); 
    $compedia=array(
     'labref'=>$labref, //NDQA201303001 
     'compedia'=>$data, 
     'specification'=>$data1 
    ); 
    foreach ($compedia as $value) { 
     $this->db->insert('coa_body',$value); 
    } 

} 

나는 그것을 반환 print_r($value) 경우 :

내가 저장하려고하면
NDQA201303001 
Array ([0] => Alphy [1] => poxy [2] => alphy [3] => poxy) 
Array ([0] => poxy [1] => alphy [2] => poxy [3] => alphy) 

하고, 그것은 반환

A Database Error Occurred 

Error Number: 1054 

Unknown column 'NDQA201303001' in 'field list' 

INSERT INTO `coa_body` (`NDQA201303001`) VALUES ('') 

Filename: C:\xampp\htdocs\NQCL\system\database\DB_driver.php 

Line Number: 330 

모든 텍스트 영역 값을 반복하여 한 번에 데이터베이스에 저장하려면 구문을 어떻게 처리해야합니까?

+0

'coa_body' 테이블의 컬럼 이름은 무엇입니까 ??? –

+0

@elav 위의 compedia 배열과 마찬가지로 labref, compedia 및 specification이 있습니다. – alphy

+0

labref는 값이 하나 뿐이지 만 compedia와 specification에는 값의 배열이 있습니다. 따라서 db에 저장하면 labref 값이 저장됩니다 (예 : NDQA201303001') 각 행마다 반복적으로 ??? –

답변

2

나는 다음이 작동

count($data) == count($data1); //Always True! 

가 그런 경우 희망 :

for ($i=0;$i<count($data);$i++) { 
    $insert_data = array(
     'labref'=>$labref, //NDQA201303001 - Same for all the rows 
     'compedia'=>$data[$i], 
     'specification'=>$data1[$i] 
    ); 
    $this->db->insert('coa_body',$insert_data); 
} 

확인이 링크 : CodePad.org


업데이트을 : 가 Rcpayan에 의해 제안 :

//This will reduce number of context switching, 
//even though loping is doubled! 
for ($i=0;$i<count($data);$i++) { 
    $insert_data[$i] = array(
     'labref'=>$labref, //NDQA201303001 
     'compedia'=>$data[$i], 
     'specification'=>$data1[$i] 
    ); 
} 
$this->db->insert_batch('coa_body',$insert_data); 
+1

고마워,이 정확히 내가 무엇을 찾고있다. 고마워요 – alphy

+0

당신은 환영합니다 ... :) –

+0

더 나은 insert_batch 사용 후입니까? – rcpayan

2

당신은 이것보다 조금 길지만 compedia명세이 같지 않을 수도 있습니다. 이 솔루션은 몇 가지 가정 : 당신은 labref의 값이

  • 하면 삽입 된 각 행에 대해 동일 할

    • compedia 및이 동일하지 사양 값의 수, 여전히 행을 삽입하려고하지만 '누락'값은 NULL으로 설정됩니다.
      $labref    = $this->uri->segment(3); 
      $compedia_data  = $this->input->post('compedia'); 
      $specification_data = $this->input->post('specification'); 
      
      //Calculate which array is larger, so we can loop through all values 
      $max_array_size = max(count($compedia_data), count($specification_data)); 
      
      //Iterate through the arrays 
      for ($i = 0; $i < $max_array_size; $i++) 
      { 
          $this->db->set('labref', $labref); 
      
          //If we still have a value(s) for compedia, then assign the value, otherwise set to NULL 
          if array_key_exists($i, $compedia_data) 
          { 
           $this->db->set('compedia', $compedia_data[$i]); 
          } 
          else 
          { 
           $this->db->set('compedia', NULL); 
          }   
      
          //If we still have a value(s) for specification, then assign the value, otherwise set to NULL 
          if array_key_exists($i, $specification_data) 
          { 
           $this->db->set('specification', $specification_data[$i]); 
          } 
          else 
          { 
           $this->db->set('specification', NULL); 
          } 
      
          //Insert into table: 'coa_body' 
          $this->db->insert('coa_body'); 
      } 
      


  • 는 다른 방법으로는 다음 배치가이 값을 삽입, 배열에 값을 할당하는 루프를 변경할 수 있습니다. 이것은 더 나은 성능을 제공 할 수 있습니다.

    //Initial other relevant code is included in the example above (excluded here for brevity) 
    $insert_array = new array(); 
    
    //Iterate through the arrays 
    for ($i = 0; $i < $max_array_size; $i++) 
    { 
        $row_array   = new array(); 
        $row_array['labref'] = $labref; 
    
        //If we still have a value(s) for compedia, then assign the value, otherwise set to NULL 
        if array_key_exists($i, $compedia_data) 
        { 
         $row_array['compedia'] = $compedia_data[$i]; 
        } 
        else 
        { 
         $row_array['compedia'] = NULL; 
        }   
    
        //If we still have a value(s) for specification, then assign the value, otherwise set to NULL 
        if array_key_exists($i, $specification_data) 
        { 
         $row_array['specification'] = $specification_data[$i]; 
        } 
        else 
        { 
         $row_array['specification'] = NULL; 
        } 
    
        //Add current row to the insert array, so it can be added to the database 
        $insert_array[$i] = $row_array; 
    } 
    
    //Insert into table: 'coa_body' 
    $this->db->insert_batch('coa_body', $insert_array); 
    
    관련 문제