简体   繁体   中英

Cookie not save in foreach

People I am trying to build a shopping cart, So i will have to pass the id , quantity and location_id to the function. In the function, i will look at the id and location_id passed in, if similar id and location_id is found. I will +1 to the item. However, if i use the function in array, it is not working. Below is another function i built to test the variables pass to function within foreach. But is does not say the first variable i passed in. it will only save the latest variable.

Below is my function codes :

static public function test($id , $per_min, $location_id_cart = 0){

    echo $new_cart = $id.'-'.$per_min.'-'.$location_id_cart;
    if(isset($_COOKIE['cart_item'])){
        $last = $_COOKIE['cart_item'];
        $testing = $last.','.$new_cart;
    }else{
        $testing = $new_cart;
    }

    $set = setcookie("cart_item", $testing, time()+3600);
    var_dump($_COOKIE);
}

Below is my codes calling the function :

    $id = 125;
    $multi_location = array(636 , 789);
    $multi_quantity = array();
    $multi_quantity[636] = 5;
    $multi_quantity[789] = 10; 
    $array_key = array_keys($multi_quantity);
    foreach($array_key as $o){
        if(!in_array($o , $multi_location)){
            unset($multi_quantity[$o]);
        }
    }
    $count = 0;
    foreach($multi_location as $i){
        ZCart::test($id , $multi_quantity[$i], $i);
    }

My result :

 'cart_item' => string '125-10-789' (length=10)

My expected result :

'cart_item' => string '125-5-636,125-10-789'

if i refresh my page, the result i get is this :

'cart_item' => string '125-10-789,125-10-789,125-10-789,125-10-789,125-10-789,125-10-789,125-10-789'

The cookie will only save the latest value given in the array. it does not save 125-5-636 to the cookie. Why? can anyone help me on this please? Thank you!

try the following code:

static public function test($id , $per_min, $location_id_cart = 0){

    $new_cart = $id.'-'.$per_min.'-'.$location_id_cart;
    if(isset($_COOKIE['cart_item'])){
        $last = $_COOKIE['cart_item'];
        $testing = $last.','.$new_cart;
    }else{
        $testing = $new_cart;
    }

    $set = setcookie("cart_item", $testing, time()+3600);
    //This line deals with adding to the cookie while the script is running
    $_COOKIE['cart_item'] = $testing;

    //var_dump($_COOKIE);
}

I have a feeling that the $_COOKIE array isn't being updated when you use setcookie() , this is why we update it manually with the line below it.

Also I'd remove the var_dump() call, along with echoing the $new_cart variable, as this will stop setcookie() from working. Instead, I would put it at the bottom of your calling code:

$id = 125;
$multi_location = array(636 , 789);
$multi_quantity = array();
$multi_quantity[636] = 5;
$multi_quantity[789] = 10; 
$array_key = array_keys($multi_quantity);
foreach($array_key as $o){
    if(!in_array($o , $multi_location)){
        unset($multi_quantity[$o]);
    }
}
$count = 0;
foreach($multi_location as $i){
ZCart::test($id , $multi_quantity[$i], $i);
}

var_dump($_COOKIE);

If you are implementing a shopping cart, I'd strongly recommend using sessions , which allow you to store the sensitive stuff on the server side away from harm and are in my experience much easier to work with than cookies!

Another option would be to store the user's cart in a database and then if they aren't logged in link to that cart with an ID stored in a cookie. This would allow the cart to stay around if the user closes their browser but keeps the secure stuff from the cookie. I hope this makes sense!

because you are not using array or different variable. Every time you call function in loop, It updates value of cart_item.It doesnot store value in different variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM