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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:07:39+00:00 2026-05-21T17:07:39+00:00

EDIT Now I can output the current product, but every time the form adds

  • 0

EDIT

Now I can output the current product, but every time the form adds another item it gets overriden. I want to make the list incremental like this:

 1. Banana 3 Units, Price 350 CRC
2. Yougurt 4 Units Price 2000 CRC
3. etc etc
4. etc

The current output only shows the last added item.

This is the script:

<?php

session_start();

//Getting the list
$list= $_SESSION['list'];


//stock
$products = array(

      'Pineaple' => 500, 'Banana' => 50, 'Mango' => 150, 
      'Milk' => 500, 'Coffe' => 1200, 'Butter' => 300,
      'Bread' => 450, 'Juice' => 780, 'Peanuts' => 800,
      'Yogurt' => 450, 'Beer' => 550, 'Wine' => 2500,
  );

//Saving the stuff
$_SESSION['list'] = array(
    'item' => ($_POST['product']), 
    'quantity' => ($_POST['quantity']),
    'code' => ($_POST['code']),
);

//price
$price = $products[($_SESSION['list']['item'])] * $_SESSION['list']['quantity'];

$_SESSION['list']['price'] = $price;


//listing
echo  "<b>SHOPPIGN LIST</b></br>";

foreach($_SESSION as $key => $item) 
{
    echo $key[''], '. ', $item['item'], ' ', $item['quantity'], ' units: ', $item['price'];
}

//Recycling list
 $_SESSION['list'] = $list;

echo "</br> <a href='index.html'>Return to index</a> </br>";


//Printing session
var_dump($_SESSION);

?>
  • 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-05-21T17:07:40+00:00Added an answer on May 21, 2026 at 5:07 pm

    This line is your issue.

    $_SESSION['list'] = array('price' => $price,);
    

    You’re setting the variable you’re trying to iterate across to be an array with a single entry in it, not to mention the fact that $price isn’t going to be a nested array, which is why trying to get item['key'] is failing (as in 'price' will be your key and $price will be your item in your foreach).

    EDIT:

    I believe, from a second quick glance you’re actually intending to do this:

    $_SESSION['list']['price'] = $price;
    

    correct me if I’m wrong.

    EDIT 2:

    Actually, looking again, I’m not quite sure I understand your structure for your $_SESSION['list'] variable. It looks like you want something like:

    (('item' => 'Banana', 'quantity' => 1...), ('item' => 'Apple', 'quantity' => 2...))
    

    but what you have (from the fact you reference $_SESSION['list']['item']) is only:

    ('item' => 'Banana', 'quantity' => 1...)
    

    you actually have multiple problems here. First try and deal with the bad structure of $_SESSION['list'] then try and deal with the foreach loop.

    EDIT 3:

    I still don’t think you’re quite understanding what I mean, so I’m just going to fix the code to be what I’m pretty sure you’re looking for…

    I’m pretty sure what you’re going for looks something like this:

    <?php
    
    session_start();
    $products = array(
          'Pineaple' => 500, 'Banana' => 50, 'Mango' => 150, 
          'Milk' => 500, 'Coffe' => 1200, 'Butter' => 300,
          'Bread' => 450, 'Juice' => 780, 'Peanuts' => 800,
          'Yogurt' => 450, 'Beer' => 550, 'Wine' => 2500,
    );
    
    if(!array_key_exists('list', $_SESSION)){
      $_SESSION['list'] = array();
    }
    
    $price = $products[$_POST['product']] * $_POST['quantity'];
    array_push($_SESSION['list'],
               array(
                 'item' => $_POST['product'], 
                 'quantity' => $_POST['quantity'],
                 'code' => $_POST['code'],
                 'price' => $price,
              ));    
    
    echo  "<b>SHOPPING LIST</b></br>";    
    foreach($_SESSION['list'] as $key => $item) {
        echo $key+1, '. ', $item['item'], ' ', $item['quantity'], ' units: ', $item['price'];
    }
    
    echo "</br> <a href='index.html'>Return to index</a> </br>";
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an edit page with url example.com/product/edit/11 Now if the validation is false
EDIT: I have fixed all but two warnings now, so thank you all for
So I know you can use output buffer. The problem right now is, I
I've been working in .NET for some time now, but occasionally I still get
EDIT: Now a Major Motion Blog Post at http://messymatters.com/sealedbids The idea of rot13 is
edit: Now for some reason each productid is only showing up once instead of
THIRD EDIT: I now believe that this problem is due to a SOAP version
EDIT: There's now a doc page on this so this question is irrelevant, also
EDIT: This code now works correctly, I only left it in case someone finds
EDIT: This question is now redundant since Twitter no longer supports basic auth. I've

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.