Jump to content


Photo

Automatically adding to an array


  • Please log in to reply
1 reply to this topic

#1 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 24 July 2006 - 06:20 AM

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:

<?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>";
}
?>

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:

<?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";}
?>

There are no error messages, but it only ever shows one of the variables rather than all of them.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#2 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 24 July 2006 - 11:22 AM

Sorry, forgot to add session_start(); to the top of the script...
Silly me!!
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users