always-a-learner
always-a-learner

Reputation: 3794

How to Update Two table Data one after other In codeigniter?

I create a model to update plan data but now with that i want to update the vendor_plan_task_mapping data because i use the plan data with vendor so when i update the plan after creating a vendor with some plan the updated plan data can't come to vendor task status mapping data hear i create a model for updating plan i have to update task_status_mapp table to be update...

how to update plan with vendor_plan_task_status_mapp table.

the model for plan update is

public function updatePlanData(){

    $planId = $this->input->post('plan_id');
    $data = array(

                    'plan_title' => $this->input->post('plan_title'),
                    'plan_price' => $this->input->post('plan_price'),
                    'plan_desc'     => $this->input->post('plan_desc')

                );

    $this->db->where('plan_id', $planId);
    $this->db->update('tbl_plan', $data);


    $this->db->where('plan_id',$planId);
    $this->db->delete('plan_task_mapping');



    foreach ($this->input->post('task_id') as $key => $value)

     {
        $data2 = array(

        'plan_id' => $planId,
        'task_id'   => $value

        );
     // echo "Index {$key}'s value is {$value}.";
           $this->db->insert('plan_task_mapping', $data2);
     }

//-------- HEAR I NEED A CODE TO UPDATE The V_T_S_M table-----------

}

after 1st table update i want to update the data in vendr_task_status_mapping table?????

Upvotes: 1

Views: 2799

Answers (3)

Jasbir Singh Sohanpal
Jasbir Singh Sohanpal

Reputation: 181

Use $this->db->insert_batch(); instead of inserting in foreach loop. Check at this link how to use it.

Upvotes: 0

bhargav Patel
bhargav Patel

Reputation: 156

IN THIS ANSWER YOU GET AN ERROR IF YOU CHANGE THE PLAN BUT USING THIS CODE YOU HAVE TO SIMPLY EDIT AND UPDATE IT ANGIN AND THIS MODEL IS CREATE A NEW ID FOR YOUR TASK AND INSERT IT AGAIN.

 public function vendorUpdateModel($data)

       {
         $vendor_id = $this->input->post('vendor_id');
         $data = array(
           'category_id' => $this->input->post('category_id'),
           'plan_id' => $this->input->post('plan_id'),
           'city_id' => $this->input->post('city_id'),
           'business_name' => $this->input->post('business_name'),
           'owner_name' => $this->input->post('owner_name'),
           'contact_no1' => $this->input->post('contact_no1'),
           'contact_no2' => $this->input->post('contact_no2'),
           'vendor_email' => $this->input->post('vendor_email'),
           'subscription_date' => $this->input->post('subscription_date'),
           'vendor_description' => $this->input->post('vendor_description'),
           'vendor_address' => $this->input->post('vendor_address')

           );

       $this->db->where('vendor_id', $vendor_id);
       $this->db->update('vendor',$data);

       $this->db->where('vendor_id',$vendor_id);
       $this->db->delete('vendor_task_status_mapping');

       $this->db->select('task_mapp_id');
       $this->db->where('plan_id',$this->input->post('plan_id'));

       $query = $this->db->get('plan_task_mapping');

       foreach($query->result() as $row)

       {
       $data2 = array(

       'task_mapp_id' => $row->task_mapp_id,
       'vendor_id'  => $vendor_id,
          'task_status'  => '0'

          );

             $this->db->insert('vendor_task_status_mapping', $data2);
        }
          return;
         }

Upvotes: 2

David Coder
David Coder

Reputation: 1138

If you added one field in plan_task_mapping table for add unique number then... As you mention your code:

$unique=array();
foreach ($this->input->post('task_id') as $key => $value)
 {
   $no=rand(0, 15);
    $data2 = array(
    'unique_no'=>$no,
    'plan_id' => $planId,
    'task_id'   => $value
    );
   $unique[] = $no; //store no in array to use it.

 // echo "Index {$key}'s value is {$value}.";
       $this->db->insert('plan_task_mapping', $data2);
 }

Before deleting plan_task_mapping table data. fetch that data.

function select()
{
    $this->db->where('plan_id',$planId);
    $Q = $this->db->get('plan_task_mapping');
    if($Q->num_rows() > 0)
    {
        foreach ($Q->result_array() as $row)
        {
            $data[] = $row;
        }
    }
    $Q->free_result();
    return $data;
}

Then delete the data as you mention in you code:

 $this->db->where('plan_id',$planId);
 $this->db->delete('plan_task_mapping');

Then delete that old data from VTSM table:

 $this->db->where('vendor_plan_task_mapping_id',$data[0]['id']); //this data[0]['id'] come form select(). change field name if required.
 $this->db->delete('VTSM');

Here fetch that new inserted data by that unique no: //which we stored it in array.

foreach($unique as $u_no)
{
   $this->db->where('unique_no',$u_no);
    $Q = $this->db->get('plan_task_mapping');
    if($Q->num_rows() > 0)
    {
        foreach ($Q->result_array() as $row1)
        {
            $plan[] = $row1;
        }
    }
    $Q->free_result();
    return $plan;
}

In above code we have fetched that new inserted data to get their id to insert status.

Now inserting status:

foreach($plan as $a)
{
    $Sdata=array(
        "plan_task_mapping_id"=>$a['id'], //this is new id change name if required
      "status"="your new status");
      $this->db->insert('VTSM',$Sdata);

}

Upvotes: 1

Related Questions