Edward Posted March 31, 2007 Share Posted March 31, 2007 Hi, I'm retrieving data from a MySQL data base, and for each row retrieved I want to make a new variable, which I can change between on/off to toggle visibility of info on my page. This is what I want to achieve, but I'm not sure how to code it... while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $variable$row['id'] = 'off'; } So if there are 3 rows, I will end up with this: while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $variable1 = 'off'; $variable2 = 'off'; $variable3 = 'off'; } Can this be done? I'm not sure if I should be using 'while' or 'for each'. Thank you. Link to comment https://forums.phpfreaks.com/topic/45050-creating-variables-with-variables-in-their-names/ Share on other sites More sharing options...
papaface Posted March 31, 2007 Share Posted March 31, 2007 http://uk.php.net/manual/en/language.variables.variable.php Link to comment https://forums.phpfreaks.com/topic/45050-creating-variables-with-variables-in-their-names/#findComment-218699 Share on other sites More sharing options...
Edward Posted March 31, 2007 Author Share Posted March 31, 2007 Thanks, now I have a set of variables whose names match the ids from my database: while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $$row['id'] = 'closed'; } $1 = 1 $2 = 2 $3 = 3 But I don't want the value to make up the entire name of the new variable, only be part of it, like this: $folder_1 = 1 $folder_2 = 2 $folder_3 = 3 I'm guessing I might need to use an array to add the prefix? Any help would be great. Link to comment https://forums.phpfreaks.com/topic/45050-creating-variables-with-variables-in-their-names/#findComment-218716 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.