$_SESSION Sub $var

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

On another page I assigned $_SESSION['1'] to 1, $_SESSION['2'] to 2...If I use echo "$_SESSION['1] "; it display everything properly, but if I try to set the sub of $_SESSION as a variable, it doesn't work. I can't figure why this isn't working.

<?php
$result2
= mysql_query($qry2);
   
    while (
$sched = mysql_fetch_array($result2, MYSQL_ASSOC))
    {
       
$tmp_id = $sched['ID'];
       
$tmp_crap = $_SESSION[$tmp_id];
              
                echo \
"$tmp_crap\"; // I get nothing!!!
      }
?>

When I echo out $tmp_crap I get a big fat nothing. What's up?

On the page I assign the sessions, this is the code I use

<?php
foreach ($_POST['courses'] as $field => $value)
{
    if (
$value) {

       
$_SESSION[\"$value\"] = $value;
    }

}
?>

EDIT: The session only works on the page where I assign the post to a session. I don't get it, all my other sessions work.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I'm not sure I understand what you want here. I see you try to retrieve $_SESSION[$tmp_id], but you don't assign it anywhere.

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

In the foreach loop the sessions are assigned, they just have a different variable as the sub. The subs in the foreach loop are 1,2,3,4 etc. When I call that session in the while loop, $tmp_id is 1,2,3 etc. The thing is, the session only works on the page where I have that foreach loop. On the foreach page, if I do <?=$_SESSION['1'] ?> I will get a one, <?=$_SESSION['2'] ?> I will get 2, etc. The session only works on the page where the sessions are assigned. It doesn't make sense to me. The second I go to the while loop page, the sessions don't work.

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Well I found the problem. Apparently I can't use numbers as the sub to sessions. It has to be something like $_SESSION['a'] not $_SESSION['1']. WTF is that about??

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

If you are going to use a number as an index, try not using quotes.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.