Add item to an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jabaro
Forum Newbie
Posts: 2
Joined: Tue Feb 10, 2009 4:16 pm

Add item to an array

Post by jabaro »

I'm fairly new to PHP, and am having trouble adding items to an array from an html form.

What I'm trying to do is allow my users to search for an item, and when they find that item, have it POST to the same page, and add that item to an array. I also want to be able to do this many times to add multiple items to the array. I can currently add one item to the array with using POST, but when I try adding subsequent items it overwrites my array each time the page loads and thus erases any previous item.

Is there a way to do this with just PHP or will I need to use a database or javaScript to facilitate this? Thanks in advance for the help!
mbdigital
Forum Newbie
Posts: 21
Joined: Tue Feb 10, 2009 7:32 am
Location: NorthWest, England

Re: Add item to an array

Post by mbdigital »

Try using a session to store your array to each time.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Add item to an array

Post by Skoalbasher »

You can turn the $_SESSION variable into an array. That stays until the session times out. Would be exactly what you're looking for I believe.

Yeah, what the above poster said.. lol. I didn't see that he had posted already.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Add item to an array

Post by John Cartwright »

Skoalbasher wrote:You can turn the $_SESSION variable into an array
Not that is matters, $_SESSION is already an array :wink:
jabaro
Forum Newbie
Posts: 2
Joined: Tue Feb 10, 2009 4:16 pm

Re: Add item to an array

Post by jabaro »

Thanks everyone for the help. Sessions were exactly what I was looking for and solved my problem nicely.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Add item to an array

Post by pickle »

Just a note that cookies are almost certainly required in order for $_SESSION to work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Add item to an array

Post by John Cartwright »

pickle wrote:Just a note that cookies are almost certainly required in order for $_SESSION to work.
Unless of course they have session.use_trans_sid enabled
Post Reply