Tubolito
Tubolito

Reputation: 19

Acessing $_SESSION in PHP

I'm creating an Inventory system in PHP

foreach ($_SESSION["cart_array"] as $each_item) { 
    $item_id = $each_item['item_id'];
    $sql = mysqli_query($mysqli, "SELECT * FROM booklists WHERE book_id='$item_id' LIMIT 1");
    while ($row = mysqli_fetch_array($sql)) {
        $product_name = $row["book_name"];

When I echo my $_SESSION using this

 print_r($_SESSION["cart_array"]);

I added 2 item in the cart with the quantity of 1 and 55

Array ( 
    [0] => Array ( [item_id] => 37 [quantity] => 1 ) 
    [1] => Array ( [item_id] => 32 [quantity] => 55 ) 
)

I wanna update all of the [quantity] values in my session. how do you think I can achieve it ?

Upvotes: 1

Views: 73

Answers (3)

Henders
Henders

Reputation: 1215

You can use a foreach loop to do this.

$newQuantityValue = 10;

foreach($_SESSION['cart_array'] as $key => $value){
    $_SESSION[$key]['quantity'] = $newQuantityValue;
}

All we do here is assign a new value for our quantity and then use a foreach loop to iterate through the $_SESSION variable so that we can update each of your quantity fields with our new value (in this case 10).

For more information about foreach loops, check out the the PHP Docs


Pass By Reference:

You could equally change the value by passing it by reference like this:

$newQuantityValue = 10;

foreach($_SESSION['cart_array'] as $key => &$value){
    $value['quantity'] = $newQuantityValue;
}

Because we are passing it by reference, there is no longer any need to reference the $_SESSION

From the docs:

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.


As a side note: You could dispense with the $newQuantityValue variable all together and instead just put the number into the loop like this:

foreach($_SESSION['cart_array'] as $key => $value){
    $_SESSION[$key]['quantity'] = 10;
}

Upvotes: 0

TMH
TMH

Reputation: 6246

The question isn't overly clear, as I don't know what the quantity needs updating by, but to update each quantity, you could do something like this.

foreach($_SESSION['cart_array'] as $index => $item){
    // Add 1 to each quantity
    $_SESSION['cart_array'][$index]['quantity']++;
}

// Or...

$increaseBy = 5;
foreach($_SESSION['cart_array'] as $index => $item){
    // Increase by a fixed amount.
    $_SESSION['cart_array'][$index]['quantity'] += $increaseBy;
}

// Or...

foreach($_SESSION['cart_array'] as $index => $item){
    // Increase by a random number between 5 and 15.
    $_SESSION['cart_array'][$index]['quantity'] += rand(5, 15);
}

// Or...

foreach($_SESSION['cart_array'] as $index => $item){
    $increaseBy = $this->getQuantityIncrease($item['item_id']);
    $_SESSION['cart_array'][$index]['quantity'] += $increaseBy;
}

// Or...
// By using references.
foreach($_SESSION['cart_array'] as $index => &$item){
    $increaseBy = $this->getQuantityIncrease($item['item_id']);
    $item['quantity'] += $increaseBy;
}

Upvotes: 1

St0iK
St0iK

Reputation: 638

You can try something like this in your loop,

foreach ($_SESSION["cart_array"] as $key => $item) { 
  $_SESSION['cart_array'][$key]['quantity'] = 13; // set the qty you want
}

Upvotes: 0

Related Questions