homepage Welcome to WebmasterWorld Guest from 54.166.148.189
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Problem adding items to an array
magicmoose




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

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.

 

PSWorx




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

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]

magicmoose




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

OMG, such a simple thing haha.
Thanks, that's been bugging me for WAY too long!:)

eelixduppy




msg:3282284
 11:42 pm on Mar 14, 2007 (gmt 0)

Glad you got everything sorted, magicmoose. And welcome to WebmasterWorld! :)

PSWorx




msg:3282314
 12:31 am on Mar 15, 2007 (gmt 0)

It was always the simple things that caught me out to start with, and indeed welcome to WebmasterWorld.com

joelgreen




msg:3282655
 12:34 pm on Mar 15, 2007 (gmt 0)

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);

PSWorx




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

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]

eelixduppy




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

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. :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved