Jump to content

Recommended Posts

Yes i looked up variable variables before I started this and from what I saw it looked fine. I cant figure out why it isn't working.

 

So what would you suggest.

 

EDIT:

 

The reason I want to do this is because I am gathering multiple variables from a database that look like this:

mens_color, mens_size, mens_price, womens_color, womens_size, womens_price, etc. (there are a fair amount more).

 

The $gender variable is set according to which we want to view. So the data would be returned using ${$gender}_color, etc.

 

It just seams a neater way than doing

if ($gender = "mens") {

$color = $mens_color }

and the same for womens. I would have a long string down the page defining variables.

Link to comment
https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368222
Share on other sites

Hey,

 

I would like to be able to select a variable using another variable. I have tried the code displayed but it does not work. Any ideas?

 

$gender = "mens";
$test_mens_test = "test";
echo $test_{$gender}_test;

 

Thanks

 

ChristianF has already mentioned the stupidity of using variable name variables, but if you're going to do it, you might as well do it right.

 

You have set the name to $test_{$gender}_test;

Try this:

$var_name = 'test_' . $gender . '_test';

echo $$var_name;

Link to comment
https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368224
Share on other sites

Or, in the pattern attempted to by the OP:

${'test_'.$gender.'_test'}

 

Though, that said. This sounds like it would have been much better solved by an array, using a structure similar to this:

$attributes = array ('mens' => array ('sizes' => ...., 'colours' => ...),
            'womens' => array ('sizes' => ...., 'colours' => ....));

And so forth.

Link to comment
https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368227
Share on other sites

Thanks for your replies.

 

 

ChristianF:

 

I did try to use an array. But mysql would not split it. I tried adding the data to the database field pd_color(eg. black,black.jpg,000000). Then i retrieved that as $color = $row['pd_color'] and tried to make an array out of it but it would not split it. I ended up having to use SPLIT_STR() and define each individual part manually.

Link to comment
https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368228
Share on other sites

Try making a function to insert the data into the database.

 

function insertData($gender, $data) {

    $sql = 'UPDATE table_name SET sizes=\'' . $data['sizes'] . '\', colors=\'' . $data['colors'] . '\' WHERE gender=\'' . $gender . '\'';

    mysql_query($sql);

}

 

$data = Array(

    'mens' => Array(

        'colors' => 'red, green, blue',

        'sizes' => '1,3,5,6,7,8,9')),

    'womens' => Array(

        'colors' => 'red, green, blue',

        'sizes' => '1,3,5,6,7,8,9')),

);

 

You can have the colours and sizes in a subarray. Use implode to put them in the database, comma seperated. Use explode to take them from the database and put them into a sub array.

Link to comment
https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368235
Share on other sites

No, no, no! Don't use store data in a database like that, please!

Use a table to hold the values, plus foreign key relations. Look up "many to many" relations, and learn how to use them. It will make your life a whole lot easier, and prevent headaches such as the one evidenced in this thread.

 

Nothadoth: If your database is storing the values in a comma-separated list, or some other "many options per column" method, then you really need to redesign the database.

Link to comment
https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368237
Share on other sites

Thanks for the replies.

 

ChristianF: Yeah I could make the database so that each value had its own field. But I figured that this would be too messy. Which is why I decided to store multiple values in a single field. Do you think that it would be better then to just put each value in its own column and have a lot of columns?

 

I'm intersted though. Why exactly do you say that it is such a problem to store data like this in a single column? And why are variable variables so bad to use?

 

Thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368308
Share on other sites

There are plenty of examples online for why not using a comma-delimited list in a database, so I won't repeat all of that here. Mainly though, it is because it defeats the purpose of the database in the first place.

 

As for variable variables, I'll just give you an example:

	$SQL[0] = "SELECT vendorname FROM vendor;";
$SQL[1] = "SELECT vendorname FROM pricetable;";
$SQL[2] = "DESCRIBE pricetable;";
$Result[0] = mysql_query ($SQL[0], $db);
$Result[1] = mysql_query ($SQL[1], $db);
$Result[2] = mysql_query ($SQL[2], $db);
$PriceList = array();
$FoundError = false;

for ($Run = 0; $OldVendor[$Run] = mysql_fetch_array($Result[1]); $Run++) {
	$OldVendorName[$Run] = $OldVendor[$Run][0];
}

for ($Run = 0; $PriceDetails[$Run] = mysql_fetch_array($Result[2]); $Run++);

while ($Vendor = mysql_fetch_array($Result[0])) {
	$VendorName = $Vendor["vendorname"];
	$Found = 0;

	for ($Run = 0; $OldVendorName[$Run]; $Run++) {
		if ($VendorName == $OldVendorName[$Run]) {
			$Found = 1;
			break;
		}
	}

	$VendorList = ${$VendorName};

 

Notice that this code is complete, in that there is no more code (that has any impact on this snippet) above this. So what you see is all there is, to this point. And yes, this has been tested and found working (albeit a long, long time ago).

Now, tell me what $VendorList contains.

 

That's why you shouldn't use variable variables. ;)

 

You're welcome, btw. Glad to hear that you're reading up on proper database relations. :)

Link to comment
https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368350
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.