CodeIgniter将多个表产品添加到单个购物车页面

时间:2016-03-14 07:18:00

标签: codeigniter

如何将多个表中的产品添加到同一购物车页面.in codeIgniter?

控制器视图

function buy()
{

        $pid = $this->uri->segment(2);
        $this->cart_model->add2cart($pid);

                    $total_products_cart    = $this->cart_model->getTotalCartProducts();
        $total_price            = $this->cart_model->getTotalCartPrice();

        $this->session->set_userdata('cart_items_count',$total_products_cart);
        $this->session->set_userdata('total_price',$total_price);
        //header("Location:".$_SERVER['HTTP_REFERER']);
                    redirect(base_url()."index.php/cart/added");
        exit();
}

模型视图

function add2cart($pid)
        {

                                $product_exists = $this->checkProductInCart($pid);

                if($product_exists=='No')
                    $this->add_product_in_cart($pid);   
                else

                                {
                                          $qty = $this->get_cart_product_quantity($pid);
                                          $this->update_quantity_in_cart($pid,($qty+1));
                                }

        }

购物车型号

function add_product_in_cart($pid)
        {

            $product = $this->admin_model->getProductDetails($pid);

            $data = array(
            "item_id" => $product->item_id,
            "item_price" => $product->item_price,
            "item_name" => $product->item_name,
            "item_quantity" => '1',
            "item_total_price" => $product->item_price,
            "item_image" => $product->thumbnail,
            "cart_session" => $this->session->userdata('cart_session')
            );

            $this->db->insert('wg_cart',$data);
        }

管理模式

        function getProductDetails($id)
        {

                $this->db->select("*");
                $this->db->from('wg_items');
                $this->db->where('item_id',$id);
                $this->db->join('wg_promo', 'wg_items.item_id = wg_promo.item_id','wg_items.item_price = wg_promo.item_price','wg_items.item_name = wg_promo.item_name','wg_items.item_quantity = wg_promo.item_quantity','wg_items.item_total_price = wg_promo.item_total_price','wg_items', 'wg_promo.item_image = wg_items.item_image');
                $result = $this->db->get('wg_items');
                if($result->num_rows()>0)
                return $result->row();
                else
                return 'empty';

        } 

在管理员模型中听到我正在尝试添加联接表。是正确的方式加入表听,请帮帮我。

1 个答案:

答案 0 :(得分:0)

我建议你查看shopping cart它会让你的生活更轻松。