Jump to content

Automatically adding to an array


gerkintrigg

Recommended Posts

Hi everyone.

Ive been trying to ad items to an array for a shop. I can define them by hard coding it, but for my dynamic shop, that's a bit pointless...

Here's the code that DOES add to the array:

[code]<?php
$_SESSION['flop']['2'] = 'whatever';
$_SESSION['flop']['1'] = 'whatever 2';

    foreach($_SESSION['flop'] as $item_id => $quantity)
{
$item_details=mysql_fetch_array(mysql_query("select * from items where item_id='". $item_id."'"));
    echo "Item = ".$item_details['item_title']." , Quantity: $quantity <br>";
}
?>
[/code]

This echoes two lines with the contents of each bit of data.

The problem is using an auto script to pick up user input and add that to the same array. Here's the code I have:

[code]<?php
include '../includes/db.php';
if (! isset($_SESSION['flop'])){
$_SESSION['flop']=array();
}

$new=$_REQUEST['new'];

if(isset($_SESSION['flop'][$new])){
      $_SESSION['flop'][$new]=$_SESSION['flop'][$new]+1;
  }
    else
      $_SESSION['flop'][$new] = 1;

if (isset($_SESSION['flop'])){
    foreach($_SESSION['flop'] as $item_id => $quantity)
{
$item_details=mysql_fetch_array(mysql_query("select * from items where item_id='". $item_id."'"));
    echo "Item = ".$item_details['item_title']." , Quantity: $quantity <br>";
}}
else{ echo "No items in your shopping cart";}
?>
[/code]

There are no error messages, but it only ever shows one of the variables rather than all of them.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.