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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.