Jump to content

[SOLVED] displaying a dynamic session variable name for an If statement


manwhoeatsrats

Recommended Posts

Sorry I am sort of a noob with PHP. I hope this question is not too bizzare.......

What I am trying to do is pull a string from a mySQL database and place it in an argument.  The Session variable name is based off of this string. So in the code below the value of $foo == the string from the mysql database.

example: 

String in the database is "hi"

so the argument would look like this

($_SESSION[hi] == 1)

<?php
if
($_SESSION[$foo] == 1)
  {
    echo "true";
  }

 

I hope this makes sense.....

okay.  lets say I have the following session variables set.

$_SESSION[a] = '1';

$_SESSION = '1';

$_SESSION[c] = '0';

 

I am going to connect to a mySQL database, and pull several rows of code.  One field in the table is the name of a session variable.  Lets call this field session_name.

 

<?php
if
($_SESSION[session_name] == '1')
  {
   echo row from database;
  }
?>

 

this is going to be part of a while do loop... The first thing that happens is the value from the field session_name is pulled from the database.  The session_name needs to be converted into a session variable name.

So lets say the value of session_name is  "a"

then in the argument it would go

($_SESSION[a] == '1')

based on teh answer of the argument it will display the row from the database.

I hope this makes some kind of sense of what I am trying to do.

 

What you posted in the first post  ($_SESSION[$foo] == 1) works because you used a php variable as the index name.

 

What you just posted  ($_SESSION[session_name] == '1') does not work because there is no php variable involved in the index name.

 

We cannot help you when you don't post your actual code.

I will write out the code, when I get back to my main computer.  For now prehaps I am making the issue to complex. 

 

$_SESSION[name is another variable]

 

I need to be able to dymanically state the session variable name. in an argument

Okay now I REALLY feel like an idiot!

I could have sworn I tried something like $_SESSION["$foo"] MULTIPLE times!  When I got home I decided to try what Deived said even though "I knew it would not work"......it did....  here is my code

<?php
session_start();
$_SESSION[a] = '1';
$foo="a";
if
($_SESSION["$foo"] == '1')
  {
echo "hi!";
  }

?>

The output was "hi!".....

Thanks for the help guys, I hope I was not to much of a pain in the backside.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.