0
votes

After completing process of data in temporary(costing_detail_temp) table,
I have to insert that data into a row in permanent(costing_detail) table with some additional column values. costing_detail table has same columns of costing_detail_temp with some additional columns. So, I have to do the following :

  1. Run a query into costing_detail_temp and select a row,
  2. Add some key-value pair into the returned array,
  3. Insert into costing_detail. So, here is the code of my model:

        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;
        }
     

This returns the following array:

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
        )
)

However,I need something like this :

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
        )
)

How can i achieve this?

2

2 Answers

1
votes

Try using foreach for you to get the result you wanted to get and insert it into the array like the example bellow:

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_itemlist);
            return $data_itemlist;
        }

OUTPUT

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
)
1
votes

try this code.

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;
}