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 9320507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:39:03+00:00 2026-06-19T03:39:03+00:00

I am storing a Shopping Cart in a PHP Session and then loading it

  • 0

I am storing a Shopping Cart in a PHP Session and then loading it to show the items in the cart.

The issue is that currently, I am seeing each of the items without consolidation, meaning if an item is added 2 times in the cart, it will be shown twice.

How could I consolidate the results by itemId?

if(is_array($_SESSION['cart']))
  {
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++)
      {
        $pid=$_SESSION['cart'][$i]['itemId'];
        $q=$_SESSION['cart'][$i]['qty'];
        if($q==0) continue;
        $query2 = $con -> prepare("SELECT * FROM item_descr WHERE id_item = :idItem");
        $query2-> bindValue (':idItem',$pid);
        $query2->execute();
        $row2 = $query2->fetch(PDO::FETCH_ASSOC);
            ?>
    <div class="checkoutItems">     
    <span class='itemNameChck'><?php echo $row2['name']; ?> </a></span>
    <span class="itemQtyChck"><?php echo $row2['price']; ?> </span>
    <span class="itemQtyChck"><?php echo $q; ?> </span>
    <span class="itemQtyChck"><?php $totalPerItem = $q * $row2['price']; echo $totalPerItem; ?> </span>
    </div>
    <?php 
      }
  }

Here is how I add the items to the cart:

function addtocart($pid,$q)
  {

    if($pid<1 or $q<1) return;
    if(is_array($_SESSION['cart']))
      {

        $max=count($_SESSION['cart']);
        $_SESSION['cart'][$max]['itemId']=$pid;
        $_SESSION['cart'][$max]['qty']=$q;
        $max=count($_SESSION['cart']);
      }
      else
      {
        $_SESSION['cart']=array();
        $_SESSION['cart'][0]['itemId']=$pid;
        $_SESSION['cart'][0]['qty']=$q;
        $max=count($_SESSION['cart']);

      }
    //}
  }
  • 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-19T03:39:05+00:00Added an answer on June 19, 2026 at 3:39 am

    In my opinion – you have to get rid of that ID that contains cart item index.

    //             I mean this
    //                 ||
    //                 ||
    //                \  /
    //                 \/
    $_SESSION['cart'][$max]['qty']=$q;
    

    Just put itemId there, and before you add item – check if this element of array is set.

    You wrote this code, that adds new element to array everytime, and now you want to consolidate. It makes no sense for me. Add new element only when you have to. Otherwise, just update quantity.

    In my opinion your cart should look like this:

    $_SESSION['cart'][$itemid]['qty']=$q;
    
    // example how to store other attributes
    $_SESSION['cart'][$itemid]['name']="Rubber duck";
    $_SESSION['cart'][$itemid]['color']="Yellow";
    $_SESSION['cart'][$itemid]['size']="XXL";
    $_SESSION['cart'][$itemid]['price']="1000";
    

    Then, when adding item you can check if cart item with that itemid contains any elements.

    if isarray($_SESSION['cart'][$itemid]) // when exists - add to previous quantity
        $_SESSION['cart'][$itemid]['qty']= $_SESSION['cart'][$itemid]['qty'] + $q;
    else                                   // else - set quantity
        $_SESSION['cart'][$itemid]['qty'] = $q;
    

    This is just a draft/idea, but I hope it will help you.

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

Sidebar

Related Questions

I have a checkout process for a shopping cart that is currently storing credit
I'm building a shopping cart with session state. But i've a problem that when
I've noticed that there are quite a few technologyes for storing temporary session data(don't
I have a shopping cart in my app and an underlying data structure that
I am developing a Shopping cart application and I am needing help about storing
Ok so I am currently building a simple shopping cart for my website and
I'm storing my shopping cart in the database and have a current_cart method in
For a shopping cart page, the list of items is displayed in a html
Is there a best-practice for scalable http session management? Problem space: Shopping cart kind
I'm working an a shopping cart with a page called display.php. This page is

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.