Jump to content

[SOLVED] assigning variables to data from loop


DamienRoche

Recommended Posts

Here's the issue.

 

I have a simple loop which drags all rows of data from a mysql table. What I am trying to do is assign a variable to each column within each row, relative to the amount of times the while has looped.

 

Here is where I am stuck, namely, assigning variables to the first column "fname".

 


$loop3="1";
while($row = mysql_fetch_array($data, MYSQL_ASSOC)) { 

$varFNAME = "\$fname"."$loop3";

$fnametest = $row['Employee_F_Name'];

$assignFNAME = "$varFNAME"." = "."\"$fnametest\";";

echo "$assignFNAME<br>";

$loop3 = $loop3 + 1;
?>

 

The output for that is the php code I need to function as php:

 

$fname1 = "1fname";

$fname2 = "2fname";

$fname3 = "3fname";

$fname4 = "4fname";

$fname5 = "5fname";

$fname6 = "6fname";

 

But how can I do that?

 

Any help is greatly appreciated.

 

Thanks.

i have never used this method before but try this

replace

$assignFNAME = "$varFNAME"." = "."\"$fnametest\";";

with

$$varFNAME = $fnametest;

have you considered using an array though? like this kinda thing

$fname=array();
while($row = mysql_fetch_array($data, MYSQL_ASSOC)) { 
$fname[] = $row['Employee_F_Name'];
}
print_r($fname);

should print

array(
0 => 1fname
1 => 2fname
...
)

 

Scott.

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.