Home > Archive > PHP Language > April 2005 > Array to session problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Array to session problem
|
|
| news.xs4all.nl 2005-04-26, 8:55 pm |
| Hello php friends,
I have a question about a script. I want to make an array and each time
I call the file a variable must be added to the array. The problem is
each time i only see the last variable. As a matter of fact only the
last variable is added to the array.
What am I doing wrong?
<?php
session_start();
$_SESSION['testcounter']++;
$_SESSION['testcounter2']++;
$to_array=$_SESSION['testcounter'];
$to_array2=200+$_SESSION['testcounter'];
if(!isset($my_array))
{
$my_array = array();
}
$mijn_array[$to_array] = $to_array2;
session_register("my_array");
reset($my_array);
while (list($test_key,$test_value) = each($my_array))
{
echo "<br>";
echo "$test_key $test_value";
}
?>
Regards,
Olaf.
| |
| Oli Filth 2005-04-26, 8:55 pm |
| news.xs4all.nl wrote:
> Hello php friends,
>
> I have a question about a script. I want to make an array and each time
> I call the file a variable must be added to the array. The problem is
> each time i only see the last variable. As a matter of fact only the
> last variable is added to the array.
>
> What am I doing wrong?
>
> <?php
> session_start();
>
> $_SESSION['testcounter']++;
> $_SESSION['testcounter2']++;
^
^
What is $_SESSION['testcounter2'] used for?
>
> $to_array=$_SESSION['testcounter'];
> $to_array2=200+$_SESSION['testcounter'];
>
> if(!isset($my_array))
^
^
Why would $my_array ever be set at this point?
> {
> $my_array = array();
> }
>
> $mijn_array[$to_array] = $to_array2;
^
^
Umm, you might want to check the name of this variable.
>
> session_register("my_array");
^
^
Don't use session_register() if you are using $_SESSION. Read the PHP
manual on how to use session variables.
>
> reset($my_array);
>
> while (list($test_key,$test_value) = each($my_array))
> {
> echo "<br>";
> echo "$test_key $test_value";
> }
> ?>
>
You can add an item to an array with:
$var[] = $value
--
Oli
| |
| Olaf Fokke 2005-04-26, 8:55 pm |
| Oli Filth wrote:
> news.xs4all.nl wrote:
>
>
> ^
> ^
> What is $_SESSION['testcounter2'] used for?
>
>
> ^
> ^
> Why would $my_array ever be set at this point?
>
>
> ^
> ^
> Umm, you might want to check the name of this variable.
>
>
> ^
> ^
> Don't use session_register() if you are using $_SESSION. Read the PHP
> manual on how to use session variables.
>
>
>
> You can add an item to an array with:
>
> $var[] = $value
>
>
Thanks for your reply!
I have read the documentation about sessions, but there is only written
how you fill a array at once.
The thing is, I want to create a kind of shopping cart.
Do you have a solution or an example.
Thank in advance.
Olaf.
| |
| Olaf Fokke 2005-04-27, 3:56 pm |
| Olaf Fokke wrote:
> Oli Filth wrote:
>
>
> Thanks for your reply!
>
> I have read the documentation about sessions, but there is only written
> how you fill a array at once.
>
> The thing is, I want to create a kind of shopping cart.
>
> Do you have a solution or an example.
>
> Thank in advance.
> Olaf.
Ok, I have rewritten the script but I still dont understand what i am
doing wrong...
The new script:
<?php
session_start();
$_SESSION['testcounter']++;
$to_array=$_SESSION['testcounter'];
if(!isset($my_array))
{
$my_array = array();
}
$my_array[$to_array] = "test";
$_SESSION['array_test']=$my_array;
reset($my_array);
while (list($a,$b) = each($_SESSION['array_test']))
{
echo "<br>";
echo "$a $b";
}
?>
Why don't I see
1 test
2 test
3 test ...
Greatings,
Olaf
| |
| Oli Filth 2005-04-27, 3:56 pm |
| Olaf Fokke wrote:
>
> Ok, I have rewritten the script but I still dont understand what i am
> doing wrong...
>
> The new script:
>
> <?php
> session_start();
>
> $_SESSION['testcounter']++;
>
> $to_array=$_SESSION['testcounter'];
>
> if(!isset($my_array))
> {
> $my_array = array();
> }
>
> $my_array[$to_array] = "test";
>
> $_SESSION['array_test']=$my_array;
You're replacing $_SESSION['array_test'] with a brand new array
(containg only one entry) each time you run the script. Try:
$_SESSION['array_test'][$to_array] = "test";
--
Oli
| |
| ofokke 2005-04-28, 3:58 pm |
| Oli Filth wrote:
> Olaf Fokke wrote:
>
>
>
> You're replacing $_SESSION['array_test'] with a brand new array
> (containg only one entry) each time you run the script. Try:
>
> $_SESSION['array_test'][$to_array] = "test";
>
>
Oli, thank you very much!!!
It's working now!
Greatings,
Olaf.
Final script:
<?php
session_start();
$_SESSION['testcounter']++;
$to_array=$_SESSION['testcounter'];
if(!isset($my_array))
{
$my_array = array();
}
$_SESSION['array_test'][$to_array] = "test";
reset($my_array);
while (list($a,$b) = each($_SESSION['array_test']))
{
echo "<br>";
echo "$a $b";
}
?>
| |
| Oli Filth 2005-04-28, 8:56 pm |
| ofokke wrote:
> Oli Filth wrote:
<SNIP>
> Oli, thank you very much!!!
>
> It's working now!
>
> Final script:
>
> <?php
> session_start();
>
> $_SESSION['testcounter']++;
>
> $to_array=$_SESSION['testcounter'];
>
=============================
> if(!isset($my_array))
> {
> $my_array = array();
> }
=============================
You can do away with these lines.
> $_SESSION['array_test'][$to_array] = "test";
>
=============================
> reset($my_array);
=============================
And this one!
> while (list($a,$b) = each($_SESSION['array_test']))
> {
> echo "<br>";
> echo "$a $b";
> }
> ?>
Also, if you're not bothered about giving each entry a specific index
key, you can just do:
<?php
session_start();
$_SESSION['array_test'][] = 'test';
....
?>
--
Oli
|
|
|
|
|