Jump to content

Creating Variables Based On Title


jj20051

Recommended Posts

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.

Link to comment
Share on other sites

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");
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

<?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");
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

admin@test.com      d65dh3            Nicholas          Someone

 

It needs to create a variable for each ($email_address = "admin@test.com", $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.

Link to comment
Share on other sites

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.

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.