Jump to content

Creating variables with variables in their names?


Edward

Recommended Posts

 

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.

 

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.

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.