aebstract Posted April 14, 2010 Share Posted April 14, 2010 I'm really kind of lost when I get to this stuff. I'm trying to loop through some database results, each one will have a unique number. I need to put it on to a variable name. For example: Each result I need to create the variable $pos#, # = the number grabbed from the database. Then later I can place them where I need them. It's going to be like 1-10 and I'd like to be able to call to them individually. $pos$r[qp] = "info"; This would basically be the idea. Quote Link to comment https://forums.phpfreaks.com/topic/198540-help-with-variable-variables-please/ Share on other sites More sharing options...
gerkintrigg Posted April 14, 2010 Share Posted April 14, 2010 If you're getting them from a database you'll be using something like: $q="SELECT * FROM `xxxxxxxxx` WHERE `id`='xxxxxxxx'"; $sql=mysql_query($q); then just do something like: #set a variable: $i=1; while($r=mysql_fetch_array($sql)){ $my_var[$i]=$r['field_name']; } then you might want to output them: foreach ($my_var as $key => $value){ echo '$'.$key.' = $_POST[\''.$key.'\'];<BR>'; } i think that'd work - not tested it though. Quote Link to comment https://forums.phpfreaks.com/topic/198540-help-with-variable-variables-please/#findComment-1041845 Share on other sites More sharing options...
fivestringsurf Posted April 14, 2010 Share Posted April 14, 2010 Not exactly sure what you're after but this example might help...It shows how to get db data and write variables using variable variables. The variables created are the field(column) names themself and each will equal their value: $sqlCommand = "SELECT * FROM table WHERE someField ='something' "; $query = mysqli_query($sqlConnect, $sqlCommand); while ($row = mysqli_fetch_assoc($query)) { foreach($row as $key =>$val){ $$key = $val; } } Quote Link to comment https://forums.phpfreaks.com/topic/198540-help-with-variable-variables-please/#findComment-1041848 Share on other sites More sharing options...
aebstract Posted April 14, 2010 Author Share Posted April 14, 2010 Not exactly sure what you're after but this example might help...It shows how to get db data and write variables using variable variables. The variables created are the field(column) names themself and each will equal their value: $sqlCommand = "SELECT * FROM table WHERE someField ='something' "; $query = mysqli_query($sqlConnect, $sqlCommand); while ($row = mysqli_fetch_assoc($query)) { foreach($row as $key =>$val){ $$key = $val; } } I don't think this is going to do it. When I do my while loop, I just want to create a single variable: $pos(valueinthecolumn'qp') = "stuff"; I want to take the value from the column qp, and attach it to the word pos and create a variable. Essentially as it loops through, it should create: $pos1, $pos2, $pos3, etc Quote Link to comment https://forums.phpfreaks.com/topic/198540-help-with-variable-variables-please/#findComment-1041851 Share on other sites More sharing options...
PFMaBiSmAd Posted April 14, 2010 Share Posted April 14, 2010 Just use an array. Variable variables are three times slower than using an array. Quote Link to comment https://forums.phpfreaks.com/topic/198540-help-with-variable-variables-please/#findComment-1041856 Share on other sites More sharing options...
aebstract Posted April 14, 2010 Author Share Posted April 14, 2010 True, thanks Quote Link to comment https://forums.phpfreaks.com/topic/198540-help-with-variable-variables-please/#findComment-1041871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.