开发者

loading and handling settings in a php we bsite

开发者 https://www.devze.com 2023-04-09 23:55 出处:网络
How would I load and handle a large amount of settings. Where I can enable and disable options. So lets say I have a table in mysql called php_settings and then I have 100\'s of fields that relate to

How would I load and handle a large amount of settings. Where I can enable and disable options. So lets say I have a table in mysql called php_settings and then I have 100's of fields that relate to the web site.

开发者_StackOverflow中文版

How would I load them in and then assign them to an array() I guess is the bast way and then use those settings throughout my web site. I didn't want to have to load them in on every page load. So ?


The web is stateless - unless you want to use a $_SESSION variable to hold all those config options (I wouldn't recommend it) you're going to have to load those you want to use on every page... on every page.

Personally I'd create a sort of site "ini" php file containing an app config Singleton class that bootstraps everything you need to launch the site, including the starting the session, connecting to the database and loading in any config settings (many of which may come from the database).

Then all you need do at the top of any new page is something like...

require_once 'path/to/app.ini.php';
$oApp = App::getInstance();

You can then access your application settings through something like $oApp->config('setting').

To cut down on the load on the database you can enable caching on the database server (as the config options are unlikely to change often) or read the values from the database but instead of using them there and then serialize the response and save it to a text file (outside the web tree) - then only return to the database IF the (cached) text file isn't present or has expired; it's slightly less of a bottleneck to go to the filesystem rather than the database server.


For example, once a minute, run a cron which will take these variables into your specified config file

$result = mysql_query("SELECT * FROM php_settings");
while($one = mysql_fetch_assoc($result)){
    $config[$one['name']] = $one['value'];
}


You should not load all settings at once if you're not gonna need them all. Loading and using a few hundred items in an array isn't going to be a huge performance hit, but if you only need two or three, why not query them when they're needed?


If you are using a table for settings you can use the oops concept as follows

create a file which contains following

class yourclassname
{

  public congif=array();
   function __construct(Settingarray[])
  {
    run your query here and select the appropriate fields and assign them to the class property linke following
     $this->congif='Your query result';
  }
}

create the object of above class like following

 obj1=new yourclassname('array of setting fields you required on that page');

}

0

精彩评论

暂无评论...
验证码 换一张
取 消