Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8055063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:17:07+00:00 2026-06-05T08:17:07+00:00

I am trying to use session variables in an e-commerce website. The idea is

  • 0

I am trying to use session variables in an e-commerce website. The idea is to start a session if one is not started already. If there’s already a session, then i want to check if the new item that was added already exists in the session array. If so, then i’d just increase the quantity, if not then append to the current session array.

Problem is that appending does not work. I get the first and second arrays, but instead of adding a third the second array keeps getting overwritten anytime the add to cart button is clicked. Here is my code

<?php 

                if(isset($_SESSION['cart'])){
                    $newItems = '';
                    $cart = $_SESSION['cart'];
                    foreach($cart as $item => $value){
//$size, $colour, $quantity and $price are the current $_GET values
                        if($prodId == $value['prodId'] && $value['colour'] == $colour && $value['size'] == $size && $value['price'] == $price){
                            $value['qty']+= $quantity;
                            }else{
                                $newItems = array('prodId'=>$prodId,'price'=>$price,'qty'=>$quantity,'colour'=>$colour,'size'=>$size);
                                $cart []= $newItems;
                                }
                        }
                    }else{
                        $_SESSION['cart'] []= array('prodId'=>$prodId,'price'=>$price,'qty'=>$quantity,'colour'=>$colour,'size'=>$size);
                        }

?>

Really don’t know where it went wrong, i’ve such rotten luck with multidimensional arrays. Any help will be most appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:17:08+00:00Added an answer on June 5, 2026 at 8:17 am

    You assigned $_SESSION['cart'] to a variable and then worked with that. This is only a copy and not a reference to your original variable. If you want to access your $_SESSION again and manipulate it, you can achieve this that way:

    $cart = $_SESSION['cart'];
    foreach($cart as $item => $value){
        if($prodId == $value['prodId'] && $value['colour'] == $colour && $value['size'] == $size && $value['price'] == $price){
            $_SESSION['cart'][$item]['qty']+= $quantity;
        }else{
            // ...
    

    — or —

    You simply assign $cart to $_SESSION['cart'] again after manipulating $cart.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use session variables to limit the access to a page. I
I am trying to use PHP session without using cookies. I have enabled session.use_trans_sid
I am trying use an if statement and TWO .addClass background-color variables based on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying to filter use a form to filter a database then provide
I'm trying to use session[:user.return_to] but with no success... My code: def after_sign_in_path_for(resource) (session[:user.return_to].nil?)
Trying to save the $_SESSION['uID'] within the session to use for functions like pulling
background: trying to use the twitter gem for ruby-on-rails. in routes: map.resources :twitter_sessions map.finalize_twitter_sessions
I was trying use svn to find a checkin using svn log, but it
I was trying use a set of filter functions to run the appropriate routine,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.