Search code examples
codeigniter-3

How do I edit a cart order by adding more items and updating existing data in mysql with codeigniter..?


This code gets the existing cart content, add more items and try to update the data or insert new data

if ($cart = $this->cart->contents()) {
            foreach ($cart as $key => $value) {
                $order_details = array(
                    'oid' => $id,
                    'p_id' => $value['id'],
                    'qty' => $value['qty'],
                    'price' => $value['price'],
                    'total_amnt' => $value['qty'] * $value['price'],
                    'created_at' => date('Y-m-d h:i:s')
                );
                $q = $this->db->where('oid',$id)->get('order_details')->num_rows(); // oid is a foreign Key inside order_details table
                if ( $q > 0 ){
                    $this->db->where('oid',$id); // oid is a foreign Key inside order_details table
                    $this->db->update('order_details',$order_details);
                } else {
                    $this->db->set('oid', $id); // oid is a foreign Key inside order_details table
                    $this->db->insert('order_details',$order_details);
                }
            }
        }

Solution

  • I simple load the existing data into cart and clear update the data in the database when saving. Another method was to delete the existing data and save the new one as current data..