duanjuebiao6730 2016-10-15 11:33
浏览 23
已采纳

一次为单个外键插入多个记录

based on insert multiple rows using one forigenk value in form

enter image description here

according to above picture there is dropdown for select project.once select project and user will be able to add other details. now i want to do when form is submitting project_id (id map with project name in drop down)should insert with other rows.
but in my case project_id is inserting with only very first row. below result came when i print the array .

Array ( [0] => Array ( [project_id] => 1 [staff_id] => 2 [item_no] => 1 [description] => 1 [qty] => 1 [unit] => cube [rate] => 1 [laboure_hrs] => 1 [laboure_cost] => 1 [amount] => 2 ) 1 => Array ( [project_id] => [staff_id] => 2 [item_no] => 2 [description] => 2 [qty] => 2 [unit] => sq.ft [rate] => 2 [laboure_hrs] => 2 [laboure_cost] => 2 [amount] => 8 ) [2] => Array ( [project_id] => [staff_id] => 2 [item_no] => 3 [description] => 3 [qty] => 3 [unit] => cube [rate] => 3 [laboure_hrs] => 3 [laboure_cost] => 3 [amount] => 18 ) )

how i can pass project_id for another fields. my code is below

controler

public function create(){

//    validate fields
            $this->form_validation->set_rules('work_product_id', 'Work Product Id', 'required');
            $this->form_validation->set_rules('work_item_description', 'Work Item Description', 'required');
            $this->form_validation->set_rules('quantity', 'Quantity', 'required');
           $this->form_validation->set_rules('rate', 'Rate', 'required|numeric');
           $this->form_validation->set_rules('laboure_hrs', 'Laboure Hrs', 'required|numeric');
            $this->form_validation->set_rules('laboure_cost', 'Laboure Cost', 'required|numeric');

           if ($_POST) 
   {
        $project_id=$this->input->post('project');
        $staff_id=$this->input->post('staff_id');
        $item_no=$this->input->post('work_product_id');
        $description=$this->input->post('work_item_description');
        $qty=$this->input->post('quantity');
        $unit=$this->input->post('unit');
        $rate=$this->input->post('rate');
        $laboure_hrs=$this->input->post('laboure_hrs');
        $laboure_cost=$this->input->post('laboure_cost');
        $amount=$this->input->post('txtmultTotal');
           $data= [];

   for ($i = 0; $i < count($this->input->post('work_product_id')); $i++)
    {
       $data[$i] = array(
           'project_id' => $project_id
           'staff_id' => $staff_id[$i],
           'item_no' => $item_no[$i],
           'description' => $description[$i],
           'qty' => $qty[$i],
           'unit' => $unit[$i],
           'rate' => $rate[$i],
           'laboure_hrs' => $laboure_hrs[$i],
           'laboure_cost' => $laboure_cost[$i],
           'amount' => $amount[$i],
        );
       }
        print_r($data);
       $this->boq_model->create($data);
    }      

}

model

function create($data){

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

        }
  • 写回答

2条回答 默认 最新

  • douchujian8124 2016-10-15 13:08
    关注

    From your html, the project_id is not a control array. So what you should do is to retrieve the project_id before the loop and use it in the loop or remove the [$i] in front of the project_id in the loop

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?