Jump to content

help with variable variables please


aebstract

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/198540-help-with-variable-variables-please/
Share on other sites

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.

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;
	}
}

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

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.