Nothadoth Posted August 9, 2012 Share Posted August 9, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/ Share on other sites More sharing options...
Christian F. Posted August 9, 2012 Share Posted August 9, 2012 This is called variable variables, and is in almost all instances a clear indicator that you're doing something the Wrong Way. I strongly recommend to rethink your approach, and find a different (and cleaner) way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368221 Share on other sites More sharing options...
Nothadoth Posted August 9, 2012 Author Share Posted August 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368222 Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368224 Share on other sites More sharing options...
Christian F. Posted August 9, 2012 Share Posted August 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368227 Share on other sites More sharing options...
Nothadoth Posted August 9, 2012 Author Share Posted August 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368228 Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368231 Share on other sites More sharing options...
Nothadoth Posted August 9, 2012 Author Share Posted August 9, 2012 maxudaskin: Yes I was going to mark as solved. However I was waiting to see if ChristianF (or anyone) happened to know a way around the problem in my last post, since everyone was saying my original method wasn't very good. Never mind. Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368233 Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368235 Share on other sites More sharing options...
Christian F. Posted August 9, 2012 Share Posted August 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368237 Share on other sites More sharing options...
Nothadoth Posted August 10, 2012 Author Share Posted August 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368308 Share on other sites More sharing options...
Nothadoth Posted August 10, 2012 Author Share Posted August 10, 2012 I am looking in to that "many to many" method now. It seams a tidy solution. I'll set about learning this. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368311 Share on other sites More sharing options...
Christian F. Posted August 10, 2012 Share Posted August 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266881-variables-help/#findComment-1368350 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.