manwhoeatsrats Posted October 8, 2009 Share Posted October 8, 2009 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..... Quote Link to comment https://forums.phpfreaks.com/topic/177012-solved-displaying-a-dynamic-session-variable-name-for-an-if-statement/ Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 .. have you tried what you posted, and does it work... Quote Link to comment https://forums.phpfreaks.com/topic/177012-solved-displaying-a-dynamic-session-variable-name-for-an-if-statement/#findComment-933313 Share on other sites More sharing options...
manwhoeatsrats Posted October 8, 2009 Author Share Posted October 8, 2009 Yes, I have tried it and without sucess. Quote Link to comment https://forums.phpfreaks.com/topic/177012-solved-displaying-a-dynamic-session-variable-name-for-an-if-statement/#findComment-933330 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 ok, well without seeing more code than those 4 lines I can't help you at all. what you are proposing to do should work theoretically Quote Link to comment https://forums.phpfreaks.com/topic/177012-solved-displaying-a-dynamic-session-variable-name-for-an-if-statement/#findComment-933332 Share on other sites More sharing options...
manwhoeatsrats Posted October 8, 2009 Author Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177012-solved-displaying-a-dynamic-session-variable-name-for-an-if-statement/#findComment-933341 Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2009 Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177012-solved-displaying-a-dynamic-session-variable-name-for-an-if-statement/#findComment-933353 Share on other sites More sharing options...
manwhoeatsrats Posted October 8, 2009 Author Share Posted October 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/177012-solved-displaying-a-dynamic-session-variable-name-for-an-if-statement/#findComment-933358 Share on other sites More sharing options...
deived Posted October 8, 2009 Share Posted October 8, 2009 I think you need $_SESSION["$foo"] if $foo == "hi", it will output as $_SESSION[hi]. The session variable should be called as $_SESSION["hi"] Quote Link to comment https://forums.phpfreaks.com/topic/177012-solved-displaying-a-dynamic-session-variable-name-for-an-if-statement/#findComment-933360 Share on other sites More sharing options...
manwhoeatsrats Posted October 8, 2009 Author Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177012-solved-displaying-a-dynamic-session-variable-name-for-an-if-statement/#findComment-933381 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.