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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.