[KLUG Members] session variables

Richard Harding rick at ricksweb.info
Sun Dec 5 09:13:20 EST 2004


Lunitix wrote:

> Is there a way to place a variable within a variable in php.
>
> ie
>
> page1
>
> session_start();
> $_SESSION['var1']= "0";
> $_SESSION['var2']= "0";
> $_SESSION['var3']= "0";
> ...etc...
> link to page2
>
>
> page2
>
> if ($var1 == 0) {
> echo "image1 with link to page3";
> } else
> echo "imageblank";
>
> if ($var2 == 0) {
> echo "image2 with link to page3";
> } else
> echo "imageblank";
>
> if ($var3 == 0) {
> echo "image3 with link to page3";
> } else
> echo "imageblank";
>
>
> page3
>
> if ($istrue)
> return to page2
> else
> link-to-make-query-true-and-reload-page;$_SESSION['here is where the 
> session_variable needs to be variable based on the link from page2']'
>
> If the $_SESSION variable on page3 didn't have to change, this works 
> easy, but there are currently 80 links on page2 that this query should 
> affect.
>
> Sorry for the mess trying to explain what I am trying to do.  But the 
> pages I'm writing on are test pages and don't look a whole lot better 
> at the moment.
>
> Jon
> Minding of Madness
>
> _______________________________________________
> Members mailing list
> Members at kalamazoolinux.org
> 

Yes, variable names can be variables. I use this a lot in my queries for 
instance like so:

$query = "SELECT * FROM psychapp WHERE ID= $a_ID";
       
            if( !($result = @ mysql_query($query, $this->dbconnection )) )
                  showError();
                 
              $row = mysql_fetch_assoc($result);
             
              foreach($row as $k=>$v)
               $this->$k = $v;  

The idea being it sets a variable in the current object for each of the 
table names returned from the query. So there's nothing wrong with setting

$_SESSION["$var3"] = "something here"

The only thing is to make sure you can later retrieve what $var3 is at 
some other time.

Rick


More information about the Members mailing list