Page 1 of 1

setting variables in a while loop

Posted: Mon Feb 02, 2004 11:24 pm
by Smeagol
Hey guys,

I have a bit of a problem.

I'd like to assign variables to equal the result of an sql query, returned in an array. for example, each time through the loop a new variable is created $variable(number 1 - 12) = the element in the returned array from the sql query.

Here's my code.

$sql = "SELECT button_text FROM ".$table." ORDER BY buttonID";
$users = mysql_query($sql);
$i = 0;
while ($user = mysql_fetch_array($users)) {
$variable(number 1 - 12) = $user["button_text"]; <--- I want to set new var here.
$i++;
}

Any thoughts or help would be appreciated.

TIA.

Posted: Mon Feb 02, 2004 11:25 pm
by markl999
${'variable'.$i} = $user["button_text"];

Though not sure why'de you not just use an array? :o

Posted: Mon Feb 02, 2004 11:29 pm
by Smeagol
fabulous... thank you :)

exactly what I was after.

Here's my new code.

mysql_select_db($db);
$sql = "SELECT button_text FROM ".$table." ORDER BY buttonID";
$users = mysql_query($sql);
$i = 0;
while ($user = mysql_fetch_array($users)) {
$i++;
${'button'.$i} = $user["button_text"];
}

Posted: Tue Feb 03, 2004 5:15 pm
by infolock
i agree with mark. why not just use an array?

Code: Select all

mysql_select_db($db); 
$sql = "SELECT button_text FROM ".$table." ORDER BY buttonID"; 
$users = mysql_query($sql); 
$buttons = array();

while ($user = mysql_fetch_array($users)) 
{ 
	$buttons[] = $user['button_text']; 
}