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

  • Home
  • SEARCH
  • 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 7880719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:12:40+00:00 2026-06-03T04:12:40+00:00

Im buliding a shopping cart and want to use a 2d array to store

  • 0

Im buliding a shopping cart and want to use a 2d array to store the items ID and quantity. When the user goes to the shopping cart I want to be able to grab the Items ID from the array and output the items details from the database

/**************** Adding to the 2d array ***********************/
    //Check to see if variable isset and then add item to shopping cart
    //$itemID is the ID of the product
    //$quantity is the quantity of the product they wish to order
    if(isset($_GET['add'])){
        $itemID = $_GET['add'];
        $quantity = $_POST['quantity'];
        $_SESSION['cart'][] = array("id" => $itemID,"quantity" => $quantity);
        header('xxx');//stops user contsanlty adding on refresh
    }

    /******************** Looping through the array  **********************/
        //need to loop through grab the item ID
        //then pull what we need from the database  

        //This is where I want to grab the id from the array and query the database

        $cart = $_SESSION['cart'];
        foreach ($cart as $value ){
        //works like it should
            foreach ( $value as $key=> $final_val ){
                echo $key;
                echo ':';
                echo $final_val;
                echo '<br/>';
            }
                echo '<br/>';
        }

The array outputs like so

id:1
quantity:5

id:2
quantity:1

Im having a little trouble figuring out how to seperate the ID and quantity so that I can query the database with the item ID.

  • 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-03T04:12:41+00:00Added an answer on June 3, 2026 at 4:12 am
    foreach ( $value as $key=> $final_val ){
                  if($key=='id')
                  {
                    echo $key;
                    echo ':';
                    echo $final_val;
                    echo '<br/>';
                  }
                }
    

    or you can directly use like $value['id']
    something like this will help you..please try.
    is this you need?

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

Sidebar

Related Questions

In my Django project I am building a shopping cart system. I want to
I'm building a shopping cart page that could potentially contain dozens of separate items.
The shopping cart I am building only seems to update the quantity for the
I am currently building a shopping cart for a client but he wants different
i am currently building a custom shopping cart to my website, it has all
I am currently building a new style ecommerce shopping cart to test various scenarios
Is there any online tutorial available for building a shopping cart in cakePHP 1.3
I am building a Web Application for shopping cart using ASP.NET and I am
I am building a custom shopping cart where CC numbers and Exp date will
I'm building a shopping cart for a school assignment. I'm pretty much there except

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.