2014-10-02 3 views
0

송장 양식을 만들고 싶습니다. 이런 이유로 나는 한 번에 여러 데이터를 삽입해야합니다. 예를 들어 DB 5 카테고리 판매를 입력하거나 한 번에 제품을 구매하려고합니다. 하지만 insert_batch에 관심이 없습니다. 나는 시도하지만 db에서 null 값을 얻었습니다.다중 데이터에 삽입 할 codeigniter 배열 및 루프

내 모델은 다음과 같습니다

<?php 
class Purchase_model extends CI_Model{ 

    public function purchase(){ 


    $price = $this->input->post('price'); 
    $quantity = $this->input->post('quantity'); 
    $date = $this->input->post('date'); 
    $vendor_name = $this->input->post('vendor_name'); 
    $model = $this->input->post('model'); 

    $invoice_no = $this->input->post('invoice'); 
    //$temp = count($this->input->post('vendor_name')); 
    for($i=0; $i<10; $i++){ 
    $data = array(
    'date'=>$date[$i], 
    'vendor_name'=>$vendor_name[$i], 
    'model'=>$model[$i], 
    'price' =>$price[$i], 
    'purchase_quantity'=>$quantity[$i], 
    'amount' =>$price[$i]*$quantity[$i], 
    'invoice_no'=>$invoice_no[$i] 
    ); 

     $insert = $this->db->insert('purchase',$data); 
    return $insert; } 
     }} 

내 컨트롤러는 다음과 같습니다 예를 들어

public function purchase(){ 

    if($this->Purchase_model->purchase()){ 

     $this->session->set_flashdata('Success', 
      'You are entered data successfully'); 
     redirect('home/purchase_form'); 
    } 
} 

내보기 :

<?php echo form_label ('Price:'); ?> 
     <select name="price[]" id="price" class="input-xlarge"> 

     </select> 



     <?php echo form_label ('Quantity'); ?> 

     <?php 
      $data = array ('name'  =>'quantity[]', 
      'class'  =>'input-xlarge', 
      'value'  => set_value('quantity')); ?> 

     <?php echo form_input ($data); ?> 

     <?php echo form_label ('Price:'); ?> 
      <select name="price[]" id="price2" class="input-xlarge"> 

      </select> 

     <?php echo form_label ('Quantity'); ?> 

     <?php 
      $data = array ('name'  =>'quantity[]', 
      'class'  =>'input-xlarge', 
      'value'  => set_value('quantity')); ?> 

     <?php echo form_input ($data); ?> 

도와주세요.

+0

Null 값은 무엇입니까? 명시 해주세요 ! – Hatem

+0

공급 업체 및 수량 – user3752230

+0

보기에 "공급 업체"요소가 없습니다! 당신의 뷰 레이어의 전체 스크립트를 보여주세요! – Hatem

답변

1

문제가 해결되었습니다. 내 모델에서의 실수. 올바른 모델은

public function purchase(){ 

$data = array(); 
$temp = count($this->input->post('quantity')); 

for($i=0; $i<$temp; $i++){ 

$invoice_no = $this->input->post('invoice'); 
$date  = $this->input->post('date'); 
$price  = $this->input->post('price'); 
$quantity = $this->input->post('quantity'); 
$vendor_name = $this->input->post('vendor_name'); 
$model  = $this->input->post('model'); 
    if( $quantity[$i]!= '') {  
$data[] = array(
    'date'=>$date, 
    'vendor_name'=>$vendor_name[$i], 
    'model'=>$model[$i], 
    'price' =>$price[$i], 
    'purchase_quantity'=>$quantity[$i], 
    'amount' =>$price[$i]*$quantity[$i], 
    'invoice_no'=>$invoice_no 
    );} } 

     $insert = count($data); 

      if($insert) 
      { 
      $this->db->insert_batch('purchase', $data); 
      } 

      return $insert; 
     } 

나를 도우려는 모든 사람에게 감사드립니다.