Presently all my script's configurations are situated inside a PHP file that we 'include'. I am along the way of moving these configurations (about 100) to some database table known as 'settings'. However I am battling to locate a competent method of locating these in to the file.

The configurations table has 3 posts:

  • ID (autoincrements)
  • title
  • value

Two example rows may be:

admin_user            john
admin_email_address   john@example.com  

The only method I'm able to think about locating each setting is much like this:

$result = mysql_query("SELECT value FROM settings WHERE name = 'admin_user'");
$row = mysql_fetch_array($result);
$admin_user = $row['value'];

$result = mysql_query("SELECT value FROM settings WHERE name = 'admin_email_address'");
$row = mysql_fetch_array($result);
$admin_email_address = $row['value'];

and so forth

Doing the work by doing this will require up a great deal of code and will probably be slow.

It is possible to better way?

100 configurations? Load them all at one time. Which will take virtually no time whatsoever. You absolutely don't want to load them individually.

$result = mysql_query('SELECT * FROM settings');
$settings = array();
while ($row = mysql_fetch_assoc($result)) {
  $settings[$row['name']] = $row['value'];
}

If you want to compartmentalize these in some way, for the way you must do it, you can put a category or something like that up for grabs after which just load all of the configurations inside a particular category.

What I recommend is abstracting this behind an item of some type:

class Settings {
  private $settings;

  public function __get($name) {
    if (!$this->settings)) {
      $result = mysql_query('SELECT * FROM settings');
      $this->settings = array();
      while ($row = mysql_fetch_assoc($result)) {
        $this->settings[$row['name']] = $row['value'];
      }
    }
    return $this->settings[$name];
  }
}

By doing this the configurations aren't loaded before you try to access one:

$settings = new Settings;
echo $settings->admin_name; // now they're loaded
$result = mysql_query('SELECT * FROM settings');
$settings = array();
while ($row = mysql_fetch_assoc($result)) {
$settings[$row['name']] = $row['value'];
}

Thanks but that appears to output the next:

john  
john@example.com

whereas it ought to be:

$admin_user = "john";  
$admin_email_address = "john@example.com";

Also, I didn't remember to say, this is in the function and so i think I have to result in the retrieved variables global?

To others:

Thanks, Used to do consider keeping the configurations within the file.

The truth is, all the configurations will have to be editable within the script's admin panel. And So I guess whenever the consumer saves their configurations within the admin panel it would need to produce a PHP file that contains the configurations which in turn overwrites the exisiting configurations file?

But will not anyone's configurations then be overwritten once they upload the following version from the script?

Well I appear to possess figured it:

$settings = mysql_query("SELECT * FROM settings");

while ($row = mysql_fetch_assoc($settings)) {
eval('global $' . $row['name'] . ';');
eval('$' . $row['name'] . ' = "' . $row['value'] . '";');
}

It really works although I wasn't interested in using eval(), however i think it's the only method.

I am now wondering whether many hosts have eval() disabled. Any ideas?