Jump to content

Archived

This topic is now archived and is closed to further replies.

gerkintrigg

Automatically adding to an array

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.

Share this post


Link to post
Share on other sites
Sorry, forgot to add session_start(); to the top of the script...
Silly me!!

Share this post


Link to post
Share on other sites

×

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.