Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Array to session problem
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.

Report this thread to moderator Post Follow-up to this message
Old Post
news.xs4all.nl
04-27-05 01:55 AM


Re: Array to session problem
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

Report this thread to moderator Post Follow-up to this message
Old Post
Oli Filth
04-27-05 01:55 AM


Re: Array to session problem
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Olaf Fokke
04-27-05 01:55 AM


Re: Array to session problem
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

Report this thread to moderator Post Follow-up to this message
Old Post
Olaf Fokke
04-27-05 08:56 PM


Re: Array to session problem
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

Report this thread to moderator Post Follow-up to this message
Old Post
Oli Filth
04-27-05 08:56 PM


Re: Array to session problem
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";
}
?>

Report this thread to moderator Post Follow-up to this message
Old Post
ofokke
04-28-05 08:58 PM


Re: Array to session problem
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

Report this thread to moderator Post Follow-up to this message
Old Post
Oli Filth
04-29-05 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:29 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.