2016-04-04 59 views
0

在完成臨時(costing_detail_temp)表中的數據處理後,
我必須將該數據插入永久(costing_detail)表中的某行並附加一些列值。 costing_detail表具有與其他列相同的costing_detail_temp列。所以,我一定要做到以下幾點:Codeigniter重建查詢輸出數組插入到另一個表

  1. 運行查詢到costing_detail_temp並選擇一排,
  2. 添加一些鍵值對到返回數組,
  3. 插入costing_detail。 所以,這裏是我的模型的代碼:

     
        function add_new_detail($Temp_id) 
        { 
         $loggedUserID = $this->session->userdata('id_user'); 
         $added_on = date('Y-m-d H:i:s'); 
         $added_date = date('Y-m-d');  
         $ip = $this->input->ip_address(); 
         $qry = "SELECT * FROM costing_detail_temp WHERE Temp_id = ".$Temp_id; 
         $query = $this->db->query($qry); 
         $data_item_list = $query->result(); 
         $additional_data = array(
          'added_by' =>$loggedUserID, 
          'added_on' =>$added_on, 
          'added_date' =>$added_date, 
          'operation_ip' =>$ip 
          ); 
         //push the array $additional_data in $data_item_list 
         array_push($data_item_list, $additional_data);  //********** 
         //insert a row with the combined array 
         $this->db->insert('costing_detail', $data_item_list); 
         return $data_item_list; 
        } 
    

這將返回以下陣列:

 
Array 
(
    [0] => stdClass Object 
     (
      [Temp_id] => 20160300101 
      [Quotation_id] => 201603001 
      [item_name] => BYA :3x 3 re (3-w) Red 
      [item_id] => 0 
      [Item_Specification] => core=3, wire=3, insulation=pvc, dia=3mm, color=red 
      [Required_Quantity] => 3.00 
     ) 

    [1] => Array 
     (
      [added_by] => 12 
      [added_on] => 2016-04-04 12:15:47 
      [added_date] => 2016-04-04 
      [operation_ip] => 127.0.0.1 
     ) 
) 

不過,我需要這樣的:

 
Array 
(
    [0] => stdClass Object 
     (
      [Temp_id] => 20160300101 
      [Quotation_id] => 201603001 
      [item_name] => BYA :3x 3 re (3-w) Red 
      [item_id] => 0 
      [Item_Specification] => core=3, wire=3, insulation=pvc, dia=3mm, color=red 
      [Required_Quantity] => 3.00 
      [added_by] => 12 
      [added_on] => 2016-04-04 12:15:47 
      [added_date] => 2016-04-04 
      [operation_ip] => 127.0.0.1 
     ) 
) 

如何我可以做到這一點嗎?

回答

1

嘗試使用foreach讓你得到你想得到並將其插入到像波紋管的例子中,數組的結果:

function add_new_detail($Temp_id) 
     { 
      $loggedUserID = $this->session->userdata('id_user'); 
      $added_on = date('Y-m-d H:i:s'); 
      $added_date = date('Y-m-d');  
      $ip = $this->input->ip_address(); 
      $qry = "SELECT * FROM costing_detail_temp WHERE Temp_id = ".$Temp_id; 
      $query = $this->db->query($qry); 

      $data_itemlist = array(); 

      foreach ($query->result() as $dat) { 
       foreach ($dat as $key => $value) { 
        $data_itemlist[$key] = $value; 
       } 
       $data_itemlist['added_by'] = $loggedUserID; 
       $data_itemlist['added_on'] = $added_on; 
       $data_itemlist['added_date'] = $added_date; 
       $data_itemlist['operation_ip'] = $ip; 
      } 

      $this->db->insert('costing_detail', $data_item_list); 
      return $data_itemlist; 
     } 

輸出

Array(
[Temp_id] => 20160300101 
      [Quotation_id] => 201603001 
      [item_name] => BYA :3x 3 re (3-w) Red 
      [item_id] => 0 
      [Item_Specification] => core=3, wire=3, insulation=pvc, dia=3mm, color=red 
      [Required_Quantity] => 3.00 
      [added_by] => 12 
      [added_on] => 2016-04-04 12:15:47 
      [added_date] => 2016-04-04 
      [operation_ip] => 127.0.0.1 
) 
+0

return $ data_item_list;未定義,請將其重命名爲$ data_itemlist –

0

試試這段代碼。

function add_new_detail($Temp_id) 
{ 
    $loggedUserID = $this->session->userdata('id_user'); 
    $added_on = date('Y-m-d H:i:s'); 
    $added_date = date('Y-m-d');  
    $ip = $this->input->ip_address(); 
    $qry = "SELECT * FROM costing_detail_temp WHERE Temp_id = ".$Temp_id; 
    $query = $this->db->query($qry); 
    $data_item_list = $query->result_array(); // result as an array 
    // add additional data 
    foreach ($data_item_list as $key => $item) 
    { 
     $data_item_list[$key]['added_by'] = $loggedUserID; 
     $data_item_list[$key]['added_on'] = $added_on; 
     $data_item_list[$key]['added_date'] = $added_date; 
     $data_item_list[$key]['operation_ip'] = $operation_ip; 
    } 
    //insert a row with the combined array 
    $this->db->insert('costing_detail', $data_item_list); 
    return $data_item_list; 
} 
+0

感謝,這也工作:),這將新元素放入數組而不是創建新數組(在選定的答案中) –

相關問題