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
https://forums.phpfreaks.com/topic/15451-automatically-adding-to-an-array/
Share on other sites

Archived

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

×
×
  • 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.