Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this messagenews.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
Post Follow-up to this messageOli 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.
Post Follow-up to this messageOlaf 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
Post Follow-up to this messageOlaf 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
Post Follow-up to this messageOli 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";
}
?>
Post Follow-up to this messageofokke 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.