elmas156 Posted May 9, 2013 Share Posted May 9, 2013 Hello everyone, This seems that it should be a fairly easy thing to do, but I'm having some difficulty. What I'm trying to query a list of items from my database, and then for each result I get, I want to make it a variable itself. Here's what I have: <?php include("../dbconnect.inc.php"); $result = mysql_query("SELECT `prodid` FROM products") or die (mysql_error()); while ($row = mysql_fetch_row($result)) { $prodid = $row[0]; // If the result of $prodid is "chair," I would like to create a variable named $chair and set its value to "n" ($chair = "n"). I // would like to do this for each result. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/277848-making-the-result-of-a-variable-a-variable-itself/ Share on other sites More sharing options...
taquitosensei Posted May 9, 2013 Share Posted May 9, 2013 use a double $$ $$row[0]="n"; Hello everyone, This seems that it should be a fairly easy thing to do, but I'm having some difficulty. What I'm trying to query a list of items from my database, and then for each result I get, I want to make it a variable itself. Here's what I have: <?php include("../dbconnect.inc.php"); $result = mysql_query("SELECT `prodid` FROM products") or die (mysql_error()); while ($row = mysql_fetch_row($result)) { $prodid = $row[0]; // If the result of $prodid is "chair," I would like to create a variable named $chair and set its value to "n" ($chair = "n"). I // would like to do this for each result. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/277848-making-the-result-of-a-variable-a-variable-itself/#findComment-1429340 Share on other sites More sharing options...
gizmola Posted May 9, 2013 Share Posted May 9, 2013 I'm not sure this is a good idea, however, what you want to do can be done with variable variables. The question that has to be asked is... how will you know that a variable '$chair' even exists? Instead, why wouldn't you want to simply store an array key in an array. With the array you can easily foreach through it. variable variables while (....) { $name = $row[0]; $$name = 'n'; // If $name = 'chair', you now have a variable $chair available. } echo $chair; Why not this however? $mystuff = array(); while (....) { $mystuff[$row[0]] = 'n'; } foreach ($mystuff as $key => $value) { echo "$key: $value<br>\n"; } Considering your statement that you want to set all the values to 'n', it's hard to understand what you're going for here. Quote Link to comment https://forums.phpfreaks.com/topic/277848-making-the-result-of-a-variable-a-variable-itself/#findComment-1429343 Share on other sites More sharing options...
elmas156 Posted May 9, 2013 Author Share Posted May 9, 2013 I understand how it's difficult to understand what I'm going for here, because it's difficult for me to explain what I'm going for here. Basically, I'm trying to edit an existing page to have more functionality without having to re-write the entire page. My php knowledge is limited, but I've always found a way to make my code work. Not saying that it's always the best way to do things, but at least it works. Just so I understand your array alternative, is this how the code would look (I don't have much experience with arrays)? <?php include("../dbconnect.inc.php"); $result = mysql_query("SELECT `prodid` FROM products") or die (mysql_error()); $products = array(); while ($row = mysql_fetch_row($result)) { $products[$row[0]] = 'n'; } foreach ($products as $key => $value) { echo "$key: $value<br>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/277848-making-the-result-of-a-variable-a-variable-itself/#findComment-1429351 Share on other sites More sharing options...
Jessica Posted May 9, 2013 Share Posted May 9, 2013 Try it and see. Quote Link to comment https://forums.phpfreaks.com/topic/277848-making-the-result-of-a-variable-a-variable-itself/#findComment-1429356 Share on other sites More sharing options...
Solution elmas156 Posted May 9, 2013 Author Solution Share Posted May 9, 2013 I did try it. Still trying to understand it, but *I'm still researching it* ;-) Quote Link to comment https://forums.phpfreaks.com/topic/277848-making-the-result-of-a-variable-a-variable-itself/#findComment-1429361 Share on other sites More sharing options...
Jessica Posted May 9, 2013 Share Posted May 9, 2013 You may want to look into a templating system. Quote Link to comment https://forums.phpfreaks.com/topic/277848-making-the-result-of-a-variable-a-variable-itself/#findComment-1429389 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.