Hi all im hoping that some one can help me. what I am trying to do is add strings to a session array using the push function, however every time a new string is pushed it over rides the original so the array only ever has one element (element [0]). I know this maybe simple but if someone could give me some simple to understand sample code or talk me through it step by step i will be most grateful. ;D
Hi.
My guess is that you’re not creating an array in the session variable you’re trying to use.
Here’s how you should be able to get it to work:
[php]
$_SESSION[‘myArray’] = array();
[/php]
Now you can add items to it using:
[php]
$_SESSION[‘myArray’][] = ‘string1’;
$_SESSION[‘myArray’][] = ‘string2’;
[/php]
This syntax of [] is better than push according to the php manual if all you are doing with it is adding a new element at the end of the array.
I hope this helps.
hi this is what i got, the problem is every time the button is pressed the string changes and it overrides the element but i want it to add another.
[php]
<?php $_SESSION['myArray'] = array(); $_SESSION['myArray'][] = $_REQUEST["name"]; ?> name: <?phpprint_r($_SESSION[‘myArray’]);
?>
[/php] please help ;D and thanksBefore
[php]
$_SESSION[‘myArray’] = array();
[/php]
Add:
[php]
if (!isset($_SESSION[‘myArray’]))
[/php]
Each time the page loads, the session variable is re-defined as an empty array.
By adding this line, it will only be defined if it doesn’t exist yet.
still not working it just overrides the element each time i have been stuck on this for months now but thanks for the help so far.
Oh, duh - i just looked at the whole code…
Start the file with:
[php]
<?php session_start(); ?> ...[/php]If you don’t have session_start in there, the session variable is NOT saved, so it gets recreated each time.
Sorry I didn’t see this right away…
Thank you Thank you Thank you ;D ;D ;D
its working i am so great full .
You’re welcome!
Glad it’s working now
you deserve your karma points increasing for that
No worries, I was at the right place at the right time