Welcome to WebmasterWorld Guest from 54.145.144.101

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Problem adding items to an array

   
10:38 pm on Mar 14, 2007 (gmt 0)

5+ Year Member



Hi, I have been trying to learn php for the last 3 weeks or so and I have a problem I cannot work out. I am trying to create a list (shopping cart of items) which are contained within an array and which can have new items added via a form. I then just want the contents of the array to be displayed in a simple list.
I can get one item to be added, but when I try to add another item, the new item just replaces the old one. If anyone can see what's going wrong here could you please give me a nudge in the right direction:)
Here's the code I am using.

<? session_start();?>
<?
if (isset($_POST['add'])) //if an item was added
{
$selected=$_SESSION['selectedcomic']; //the item selected
if (!isset($SESSION['basket'])) //if array dosent exist, create it
{
$_SESSION['basket'] = array();
$_SESSION['basket'][] = $selected; //add item to array
}else{
$_SESSION['basket'][] = $selected;
}
}

foreach ($_SESSION['basket'] as $item) { //display contents of array
echo "$item<br />";
}
?>

Thanks in advance.

10:50 pm on Mar 14, 2007 (gmt 0)

10+ Year Member



You need to correct the variable name in your!isset() statement it must read $_SESSION

[edited by: PSWorx at 11:02 pm (utc) on Mar. 14, 2007]

11:03 pm on Mar 14, 2007 (gmt 0)

5+ Year Member



OMG, such a simple thing haha.
Thanks, that's been bugging me for WAY too long!:)
11:42 pm on Mar 14, 2007 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Glad you got everything sorted, magicmoose. And welcome to WebmasterWorld! :)
12:31 am on Mar 15, 2007 (gmt 0)

10+ Year Member



It was always the simple things that caught me out to start with, and indeed welcome to WebmasterWorld.com
12:34 pm on Mar 15, 2007 (gmt 0)

5+ Year Member



magicmoose, would advise to tune php to show all error messages if it is not set so yet (including errors, warnings, and notices). This will help in learning php and will help you quickly catch some bugs. Good written program should not produce errors, warnings, and notices.

error_reporting(E_ALL);

1:02 pm on Mar 15, 2007 (gmt 0)

10+ Year Member



I agree but i dont think that would have helped here as it was looking for the var $SESSION being set, which it wont have been, then it goes to create $_SESSION, and essentialy, with the origonal script just creates a new session every time its run.

Again i do agree with the error reporting, make it as reporty as possible, if its a dev box edit your php.ini like to this and restart your http service:

error_reporting = E_ALL & E_NOTICE & E_STRICT

Dont forget to set display_errors to On

[edited by: PSWorx at 1:02 pm (utc) on Mar. 15, 2007]

1:12 pm on Mar 15, 2007 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Displaying errors while developing the site gives you a great advantage when it comes time to debug your scripts. Just remember, keeping these settings for the production site may reveal information to the public that you don't want them to see. When the site is made "live", you want to log errors, not display them. :)