jj20051 Posted November 6, 2010 Share Posted November 6, 2010 I would like to create variables based on data stored in a database using the text in the variable. This is so that users using my script can simply add custom settings to the database and automatically use them. Example as follows: Table Structure: id, name, value, group The name would become the variable name while the value would become the value of that name. Example row: 1, site_title, testing this out, site_settings Result: $site_title = "testing this out"; I know I will need to use a while() to pull the data but I don't know how to get php to automatically create a variable with the right name/data combo... Any ideas out help. Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/ Share on other sites More sharing options...
Adam Posted November 6, 2010 Share Posted November 6, 2010 Have a look into extract. Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131019 Share on other sites More sharing options...
jj20051 Posted November 6, 2010 Author Share Posted November 6, 2010 Thank you, just what I was looking for XD Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131021 Share on other sites More sharing options...
jj20051 Posted November 6, 2010 Author Share Posted November 6, 2010 In the event someone has a similar question in the future here is my exact code: <?php // Pull all settings variables from the database. $settings=array(); $setting = mysql_query("SELECT * FROM settings WHERE type='site_settings'") or die(mysql_error()); while($pull_setting = mysql_fetch_array($setting)) { $setting_name = $pull_setting['name']; $setting_value = $pull_setting['value']; $settings[$setting_name] = $setting_value; } extract($settings, EXTR_PREFIX_SAME, "wddx"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131025 Share on other sites More sharing options...
jj20051 Posted November 6, 2010 Author Share Posted November 6, 2010 Alright so I have one more question... How would I go about pulling the "column names" from mysql to allow for custom columns to be added? This is similar to my previous query except this time I'm trying to load user data. So as an example if there is a column named "username" with the data "jj20051" in it, then the array I would need would look like this: $settings[$column_name] = $column_value; How would I load the column name and how would I make sure I loaded all of the columns? (if say there are 9 columns, but that could change to 10 or 20 columns later) I'm thinking I'll need to use a for_each() but I'm unsure how I'd go about making sure it had a list of all of the number of columns. Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131033 Share on other sites More sharing options...
Adam Posted November 6, 2010 Share Posted November 6, 2010 You'd already have the data structured like that if you used mysql_fetch_assoc (or equivalent if not using MySQL). Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131036 Share on other sites More sharing options...
jj20051 Posted November 6, 2010 Author Share Posted November 6, 2010 I kind of understand where you would miss understand me :'( I want to turn the column name into the variable holding the row data... Allow me to explain: It needs to be like above (with the settings) exept this time its the column name that I need to put in the array to make into a variable... // Place column name and values into an array(); $settings[$column_name] = $column_value; // Extract converts the data in the array to equal: $column_name = "column data"; See what I'm trying to do? This way even if there are 200 columns worth of data in the user's account each one gets a variable... Example table: user_id, username, password Then the variables it would output would be $user_id, $username and $password. Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131038 Share on other sites More sharing options...
Adam Posted November 6, 2010 Share Posted November 6, 2010 Ah, I think you're talking about variable variables: $name = 'foo'; $value = 'bar'; $$name = $value; Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131040 Share on other sites More sharing options...
jj20051 Posted November 6, 2010 Author Share Posted November 6, 2010 Well that helps some, but how do I get the column names from the database? Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131044 Share on other sites More sharing options...
Andy-H Posted November 6, 2010 Share Posted November 6, 2010 <?php // Pull all settings variables from the database. $settings=array(); $setting = mysql_query("SELECT * FROM settings WHERE type='site_settings'") or die(mysql_error()); while($pull_setting = mysql_fetch_array($setting)) { $setting_name = $pull_setting['name']; $setting_value = $pull_setting['value']; $settings[$setting_name] = $setting_value; } extract($settings, EXTR_PREFIX_SAME, "wddx"); ?> exactly what your current code does. <?php // Pull all settings variables from the database. $settings = array(); $setting = mysql_query("SELECT name, `value` FROM settings WHERE type='site_settings'") or die(mysql_error()); while( $row = mysql_fetch_row($setting) ) { $settings[$row[0]] = $settings[$row[1]]; } echo '<pre>' . print_r($settings, true) . '</pre>'; //${array_key} => ${array_value} /* If key is present more than once or already exists as a variable name you will need to use $wddx_{array_key} to access the data. */ extract($settings, EXTR_PREFIX_SAME, "wddx"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131046 Share on other sites More sharing options...
Andy-H Posted November 6, 2010 Share Posted November 6, 2010 <?php $settings = array( 'title' => 'My Site', 'content' => 'text/html', 'etc' => 'blah', 'etc2' => 'blah blah' ); extract($settings, EXTR_PREFIX_ALL, 'site'); echo $site_title . '<br >' . $site_content . '<br >' . $site_etc . '<br >' . $site_etc2; ?> Output: My Site text/html blah blah blah Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131053 Share on other sites More sharing options...
jj20051 Posted November 6, 2010 Author Share Posted November 6, 2010 Way off... Here is what I'm talking about: I'd like to allow users of the script to add columns to the accounts table (where the user data is stored) and have my script load those columns into variables when the user logs on. So as an example here is my table structure and an example: email_address password first_name last_name [email protected] d65dh3 Nicholas Someone It needs to create a variable for each ($email_address = "[email protected]", $password = "d65dh3", etc) ... but lets say that someone adds another column called "address" to the table, well now I want it to create a variable $address automatically along with the other ones it was creating before. They could add 10 or 20 columns to the table, but it will still create a variable for each. Hopefully you see where I want this to go... So that I can allow the creation of custom user fields. Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131054 Share on other sites More sharing options...
Pikachu2000 Posted November 6, 2010 Share Posted November 6, 2010 Assuming I'm understanding you properly, why would you even consider allowing a user to alter the structure of a database table? Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131058 Share on other sites More sharing options...
jj20051 Posted November 6, 2010 Author Share Posted November 6, 2010 It'd be the admin editing the structure so that they could have custom fields, so that they don't have to edit the script to add custom fields. Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131059 Share on other sites More sharing options...
Adam Posted November 6, 2010 Share Posted November 6, 2010 Using an array here would be much appropriate - is there a particular reason why you need to use variables? I'm a little confused by what you're asking though. I've already told you about extract(), that should be all you need..? // assume we have a valid mysql result resource called $query while ($row = mysql_fetch_assoc($query)) { // $row now contains an array indexed by the column names extract($row); // we now have a series of variables named after the indexes (the column names) } If anybody were to alter the table structure (and you either add it to the select SQL or you're using *) that variable would be created too. Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131060 Share on other sites More sharing options...
Andy-H Posted November 6, 2010 Share Posted November 6, 2010 Nevermind. lol Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131061 Share on other sites More sharing options...
Adam Posted November 6, 2010 Share Posted November 6, 2010 I really don't see why anyone would ever want to d that... especially using a looped query. Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131062 Share on other sites More sharing options...
Andy-H Posted November 6, 2010 Share Posted November 6, 2010 Overengineered it, sorry for editing the post but I saw yours... Quote Link to comment https://forums.phpfreaks.com/topic/217931-creating-variables-based-on-title/#findComment-1131063 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.