Forums
This topic is locked
array in php session
Posted 11 Oct 2007 19:19:08
1
has voted
11 Oct 2007 19:19:08 James Wright posted:
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 <img src=../images/dmxzone/forum/icon_smile_sad.gif border=0 align=middle>
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.
That is, something like this :
<pre id=code><font face=courier size=2 id=code>
if(isset($_GET['id']))
{
$select[$i] = $_GET['id'];
$i = $i + 1;
}
foreach ($select as $selected) { echo $selected."<br />";
</font id=code></pre id=code>
The problem here 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?
Replies
Replied 13 Oct 2007 01:13:03
13 Oct 2007 01:13:03 dg coder replied:
try using session instead if you want to preserve a specific array value:
<?php
if(isset($_GET['id']))
{
$_SESSION['ctr'] = $_SESSION['ctr'] + 1;
$_SESSION['select'] = $_SESSION['select'] . " " . $_GET['select'];
}
$select = $_SESSION['select'];
foreach($select as $selected)
{
echo $selected;
}
?>
Replied 13 Oct 2007 04:34:29
13 Oct 2007 04:34:29 James Wright replied:
Actually, yesterday I went with this:
<pre id=code><font face=courier size=2 id=code>
if(isset($_GET['id']))
{
$_SESSION['select'][] = $_GET['id'];
}
if(isset($_SESSION['select']))
{
foreach($unique as $selected)
{
echo $selected.'';
}
}
</font id=code></pre id=code>
Thanks for effort anyway - I very much appreciate it!
<pre id=code><font face=courier size=2 id=code>
if(isset($_GET['id']))
{
$_SESSION['select'][] = $_GET['id'];
}
if(isset($_SESSION['select']))
{
foreach($unique as $selected)
{
echo $selected.'';
}
}
</font id=code></pre id=code>
Thanks for effort anyway - I very much appreciate it!