Jump to content

Making the result of a variable a variable itself


elmas156
Go to solution Solved by elmas156,

Recommended Posts

Hello everyone,

 

This seems that it should be a fairly easy thing to do, but I'm having some difficulty.  What I'm trying to query a list of items from my database, and then for each result I get, I want to make it a variable itself.  Here's what I have:

<?php

include("../dbconnect.inc.php");

$result = mysql_query("SELECT `prodid` FROM products") or die (mysql_error());

while ($row = mysql_fetch_row($result)) {

	$prodid = $row[0];
	
	// If the result of $prodid is "chair," I would like to create a variable named $chair and set its value to "n" ($chair = "n").  I
	// would like to do this for each result.

}

?>
Link to comment
Share on other sites

use a double $$

$$row[0]="n";

 

 

Hello everyone,

 

This seems that it should be a fairly easy thing to do, but I'm having some difficulty.  What I'm trying to query a list of items from my database, and then for each result I get, I want to make it a variable itself.  Here's what I have:

<?php

include("../dbconnect.inc.php");

$result = mysql_query("SELECT `prodid` FROM products") or die (mysql_error());

while ($row = mysql_fetch_row($result)) {

	$prodid = $row[0];
	
	// If the result of $prodid is "chair," I would like to create a variable named $chair and set its value to "n" ($chair = "n").  I
	// would like to do this for each result.

}

?>
Link to comment
Share on other sites

I'm not sure this is a good idea, however, what you want to do can be done with variable variables. The question that has to be asked is... how will you know that a variable '$chair' even exists? Instead, why wouldn't you want to simply store an array key in an array. With the array you can easily foreach through it.

 

variable variables

while (....) {
    $name = $row[0];
    $$name = 'n';  // If $name = 'chair', you now have a variable $chair available.
}

echo $chair;
Why not this however?

 

$mystuff = array();
while (....) {
    $mystuff[$row[0]] = 'n';
}

foreach ($mystuff as $key => $value) {
    echo "$key: $value<br>\n";
}
Considering your statement that you want to set all the values to 'n', it's hard to understand what you're going for here.
Link to comment
Share on other sites

I understand how it's difficult to understand what I'm going for here, because it's difficult for me to explain what I'm going for here.  Basically, I'm trying to edit an existing page to have more functionality without having to re-write the entire page.  My php knowledge is limited, but I've always found a way to make my code work.  Not saying that it's always the best way to do things, but at least it works.  Just so I understand your array alternative, is this how the code would look (I don't have much experience with arrays)?

 

<?php

include("../dbconnect.inc.php");

$result = mysql_query("SELECT `prodid` FROM products") or die (mysql_error());

$products = array();

while ($row = mysql_fetch_row($result)) {

$products[$row[0]] = 'n';

}

foreach ($products as $key => $value) {

echo "$key: $value<br>\n";

}

?>


 

 

 

 

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.