Home > front end >  Update $_SESSION array value
Update $_SESSION array value

Time:03-28

I have select option, if I change the value, I need to update the session array value.

Example, in $_SESSION I have two products

Name Qtr Price
pr1 2 12
pr2 4 15

I am displaying the array on cart page with select option to change the qty.

if I change the option qty for the first product, it's updating both the product value.

Here is my PHP code

var_dump ($_SESSION['addcart'])
array(2) { 
    ["169aw"]=> array(3) { 
        ["product_id"]=> string(3) "169" 
        ["qty"]=> string(1) "2" 
        ["price"]=> string(3) "189" 
    } 
    
    ["86aw"]=> array(3) { 
        ["product_id"]=> string(2) "86" 
        ["qty"]=> string(1) "1" 
        ["price"]=> string(3) "220" 
    }
}

Displaying the array in select field

$tot_cart = $_SESSION['addcart'];
$keys = array_keys($tot_cart);

for($i = 0; $i < count($tot_cart); $i  ) {
    foreach([$keys[$i]] as $key => $value) {
        $newqty = $tot_cart[$keys[$i]]['qty'];
    }

<form action="<?php echo $base_url?>pages/login-script.php" id="cart-qty-update">
    <input type="text" value="<?php echo $keys[$i]?>" name="cart-prd-item" >
    <select name="sel-prod-opt" id="sel-prod-opt-val" onchange="update_cart()">
        <option value="1" <?php if($newqty == 1){ echo 'selected';} ?> >1</option>
        <option value="2" <?php if($newqty == 2){ echo 'selected';} ?>>2</option>
        <option value="3" <?php if($newqty == 3){ echo 'selected';} ?>>3</option>
        <option value="4" <?php if($newqty == 4){ echo 'selected';} ?>>4</option>
        <option value="5" <?php if($newqty == 5){ echo 'selected';} ?>>5</option>
        <option value="6" <?php if($newqty == 6){ echo 'selected';} ?>>6</option>
        <option value="7" <?php if($newqty == 7){ echo 'selected';} ?>>7</option>
        <option value="8" <?php if($newqty == 8){ echo 'selected';} ?>>8</option>
        <option value="9" <?php if($newqty == 9){ echo 'selected';} ?>>9</option>
        <option value="10" <?php if($newqty == 10){ echo 'selected';} ?>>10</option>
    </select>
</form>

jQuery function to get the value of the new option

function update_cart(val){
    var cart_id = $('.cart-prd-item').val();
    var cart_qty = $('.sel-prod-opt-val').val();
    
    $.ajax({
        type: 'post',
        url: 'http://localhost/web/pages/script.php',
        data: {
            cart_id:cart_id,
            cart_qty:cart_qty
        },
        success: function (response) {
            $("#new_select").html(response); 
            window.location.replace('http://localhost/web/pages/cart.php');
        }
    });
}

PHP file to set the value

if(isset($_POST['get_option'])){
    $cart_qty = $_POST['cart_qty'];
    $cart_id = $_POST['cart_id'];
   
    $keys = array_keys($_SESSION['addcart']);
    for($i = 0; $i < count($_SESSION['addcart']); $i  ) {
        foreach([$keys[$i]] as $key => $value) {
            $_SESSION['addcart'][$cart_id]['qty'] = $cart_qty;
        }
    }
}

If i change the value of the first product, instead of updating the qty value of the first product, qty value of both the arrays has been updated

CodePudding user response:

Add in select the cart id

 <select name="sel-prod-opt"  data-cart-key="<?php echo $CartKey; ?>" id="sel-prod-opt-val">

So in the js update like that

$(".sel-prod-opt").on("change", function(){
    var value = $(this).val(),
        CartKey = $(this).attr("data-cart-key");
    //call your ajax for update the cart in session
}
// PHP File like:
$_SESSION[cart][$_POST[CartKey]]['qty'] = $_POST[value];

CodePudding user response:

As far as I understand you don't need to for or foreach. you solve this problem as following line. ofcourse if your cart_id and cart_qty is true then

if(isset($_POST['get_option'])){
  $cart_qty = $_POST['cart_qty'];
  $cart_id = $_POST['cart_id'];
  $_SESSION['addcart'][$cart_id]['qty'] = $cart_qty;
}
  • Related