DamienRoche Posted September 9, 2008 Share Posted September 9, 2008 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 https://forums.phpfreaks.com/topic/123386-solved-assigning-variables-to-data-from-loop/ Share on other sites More sharing options...
ratcateme Posted September 9, 2008 Share Posted September 9, 2008 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 https://forums.phpfreaks.com/topic/123386-solved-assigning-variables-to-data-from-loop/#findComment-637284 Share on other sites More sharing options...
DamienRoche Posted September 9, 2008 Author Share Posted September 9, 2008 Thanks! That sorted it. Do you know how I can start the array from 1 instead of 0. Right now it's [0] => 1fname [1] => 2fname etc. Thanks again. Link to comment https://forums.phpfreaks.com/topic/123386-solved-assigning-variables-to-data-from-loop/#findComment-637287 Share on other sites More sharing options...
JasonLewis Posted September 9, 2008 Share Posted September 9, 2008 $fname=array(); $i = 1; while($row = mysql_fetch_array($data, MYSQL_ASSOC)) { $fname[$i] = $row['Employee_F_Name']; $i++; } print_r($fname); It's better to start it at 0 though, it's how arrays work. Link to comment https://forums.phpfreaks.com/topic/123386-solved-assigning-variables-to-data-from-loop/#findComment-637288 Share on other sites More sharing options...
DamienRoche Posted September 9, 2008 Author Share Posted September 9, 2008 I think I'll keep it how it is then. Thanks for the pointer. Link to comment https://forums.phpfreaks.com/topic/123386-solved-assigning-variables-to-data-from-loop/#findComment-637300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.