Skip to main content
October 11, 2007
Question

create array and store in php session variable

  • October 11, 2007
  • 1 reply
  • 461 views
This is probably a simple thing but I am having trouble getting my head around it.

First: I need one solution where global variables are allowed on the host server as well as one where they are not or a solution where it does not matter

An id is listed in $GET['id'] at each visit to a specific page, but I need a way to store the previous 'id' values as well as the new one.
I wanted to create an array and store it in a session variable and add to it with each subsequent visit to the page.
Then I need to display all those values on the page.
But I just can't get it to work. Well, I can get it to work but it is a huge chunk of code with static variables. I need this to be more efficient code and not tied to an array with static values.

The problem BELOW is that I just can't seem to get this as an array, when the page is accessed it is always a single value.
Can anyone help me get this in a session variable or otherwise help get it working?


This topic has been closed for replies.

1 reply

Inspiring
October 11, 2007
Glass wrote:
> First: I need one solution where global variables are allowed on the host
> server as well as one where they are not or a solution where it does not matter

The first thing you need to understand is how PHP works. The script runs
each time the page is called, but the web is a "stateless" environment.
That means that each request to a web server is completely independent
of any other. Consequently, your script is condemned to live in
ignorance of previous requests.

The way round this is to use session variables, but remember that a
session has a limited life (usually 14 minutes).

> if(isset($_GET['id']))
> {
> $select[$i] = $_GET['id'];
> $i = $i + 1;
> }
> foreach ($select as $selected) { echo $selected."<br />";

<?php
session_start();
if (isset($_GET['id'])) {
$_SESSION['select'][] = $_GET['id'];
}
if (isset($_SESSION['select'])) {
foreach($_SESSION['select'] as $selected) {
echo $selected.'<br />';
}
}

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
October 11, 2007
Glass wrote:
> Thank you for your help. However, your superior tone is not necessary ("The
> first thing you need to understand is how PHP works.").

My deepest apologies if you felt my tone was superior. The problem with
explaining code in a public forum is that it's impossible to know how
much knowledge the other person has.

You say you were well aware that session variables needed to be used,
but didn't actually use them in your own code. That gave the (perhaps
false) impression that you didn't fully understand the stateless nature
of the web and how PHP scripts are handled. My intention was to show you
why the code you were using didn't work. Sorry if I was telling you
something you already knew.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/