CodeIgniter中的自定义购物车类显示意外行为

时间:2014-11-10 11:37:52

标签: php codeigniter

我正在使用Code Igniter开发自定义购物车。添加新项目没有问题。但是,如果我添加现有项目,而不是增加购物车中项目的数量,则根本不会发生任何事情。我只是无法弄清楚我错过了哪里!这是我的代码:

澄清一点;

假设我将商品(名称= Ray Ban,quanity = 2,price = 2)添加到购物车然后再添加 item(名称= Ray Ban,quanity = 3,price = 20)。

我希望购物车中的商品为(name = Ray Ban,数量= 5,价格= 20)。

但事实并非如此。提前致谢

test.php的

<?php class Test extends CI_Controller{


function __construct(){

    parent::__construct();

}


function index(){

    $this->load->library('Mycart');

    $item = array(

                     array(
                                        'item'      => 'Mango Fruity',
                                        'price'     => '30',
                                        'quantity'  =>  2

                        ),

                     array(
                                        'item'      => 'Mango Fruitym',
                                        'price'     => '300',
                                        'quantity'  =>  4

                        ),

                     array(
                                        'item'      => 'Mango Fruity',
                                        'price'     => '30',
                                        'quantity'  =>  3

                        )                           

        );

    $this->mycart->add_item($item);


}



} 

MyCart.php


<?php class Mycart {


public $cart_items;
public  $ci;

function __construct(){


    $this->ci           =   &get_instance();

    if($this->ci->session->userdata('cart'))

        $this->cart_items = $this->ci->session->userdata('cart');

    else

        $this->cart_items = array();


}


function add_item($items = 0){

    /* 
        Function Name      : add_item
        Function Author    : Manish Lamichhane
        Function Objective : Add items to cart

        Documentation:


        if(item is present in cart)

            quantity of the item in cart += quantity of item 

        for this it checks the item against all the items available in cart 

        if the item is present in cart,  occurrence flag is set

        after all the cart items are checked for this particular loop (inner forloop)

        once again the value of occurrence is checked

        if occurrence is set, that means the item was already in cart

        and thus the amount of this item is already increased in cart

   but if occurrence is set to 0, this means this particular item is not present in cart

        and thus this has to be added as new item in cart

    */


    if($item = 0){

        return;

    }elseif(is_array($items)){


    $item_already_in_cart = 0; //parameter to check if the added item is already in cart

        foreach($items as $item ){ 


            if(count($this->cart_items)){ //checks if cart is empty


                    foreach($this->cart_items as $cart_item){

                        if($cart_item['item'] == $item['item']){ 

                        /* if item to be added exists in cart,
                           add the quantity of the item in cart */

                        $cart_item['quantity'] += $item['quantity'];

                        $item_already_in_cart++; //flag sets if same item is encountered


                        }


                    }

/*  when control reaches here,
 it means first item to be added it check across all items in cart */

                    if($item_already_in_cart){ 

                        #checks if flag for this particular item is set
                        #if flag is set, it means the item was already in cart and thus quanitity was increased
                        #so the item need not be added in the cart

                        $item_already_in_cart = 0; 


                    }else{

                        #else if the $item_already_in_cart flag is not set, then add the item to cart

                        $this->cart_items[] = $item;

                    }

                }else{

                        $this->cart_items[] = $item; //if the cart was empty 



                }


        }

        echo "<pre>";print_r($this->cart_items);exit;   


    }else{

        return;

    }


}//add_item function ends here



}//class ends here

2 个答案:

答案 0 :(得分:0)

我想您忘记添加$ this-&gt; ci-&gt; session-&gt; set_userdata($ this-&gt; cart_items); 因此,您不能将购物车保存在会话中。 尝试添加

$this->ci->session->set_userdata($this->cart_items); 

之前

echo "<pre>";print_r($this->cart_items);exit;   

答案 1 :(得分:0)

在foreach循环中使用对变量的引用就可以了!

foreach($ this-&gt; cart_items as &amp; $ cart_item){

//代码在这里

}