开发者

saving "options" and retrieving them with php/mysql

开发者 https://www.devze.com 2023-03-02 20:56 出处:网络
This is a php/mysql question. I\'m designing a website where the administrator can choose some options like: where to align the text, font size for certain elements, what height/width of the thumbnai

This is a php/mysql question.

I'm designing a website where the administrator can choose some options like: where to align the text, font size for certain elements, what height/width of the thumbnails, etc. The options are stored in a table and retrieved with php where needed. I'm trying to accomplish something similar to how Wordpress stores optio开发者_如何转开发ns in a table. What is the best approach to doing this?


wordpress stores each option in a row in a single table, some of the option data is serialized, but at the end of the day, they are essentially stored as a key => value pair

start off by figuring out what the options you are that you wish to store and give them unique keys

eg site.background = #FFFFFF

then you could create a simple table to store it

configtable
|section|key       |value  |
|site   |background|#FFFFFF|

if you make a unique index around (section, key), then you wont end up with dupes

to load just the site settings only you might do this

$result = mysql_query("select key, value from configtable where section='site'");
$siteconfig = array()
while(list($key, $value) = mysql_fetch_row($esult)) {
    $siteconfig["$key"] = $value; // forcing key to be a string so that numeric keys don't stuff things up
}

in you code you can do this

echo <div style="background: <?php echo $siteconfig['background']; ?>

to load ALL the settings at once you might do this

$result = mysql_query('select section, key, value from configtable');
$config = array()
while(list($section, $key, $value) = mysql_fetch_row($esult)) {
    $config["$section"]["$key"] = $value; // forcing section/key to be a string so that numeric keys don't stuff things up
}

in you code you can do this

echo <div style="background: <?php echo $config['site']['background']; ?>

for user specific settings, just add a user_id column to the table to store it

configtable
|section|key       |value  |user_id  |
|site   |background|#FFFFFF|bumperbox|

hope that helps


Best way to do this is to use a JavaScript tool that transforms text area into WYSIWYG editor. Some nice ones are the CkEditor, TinyMce, and my favourate, NicEdit.

Then you can take the html output from the <textarea> field, then store it into a database after checking for things like JS injections, and escaping certain characters.


What I would do is:

I would first define what style the administrator wants (through GET or POST) and then I would echo the html and the style.

Like this:

if($_POST['textColour']=='blue')

{
  echo "<span style=\"color: blue; \">My text</span>";
}

It's been a while since I coded CSS like that but I think it works. I'm sure there are other ways.


You could have a class called Variables like this

class Variables {
  public function __get($key) {
    // fetch from cache/table
  }
  public function __set($key,$value) {
    // store to cache/table
}

See the PHP man page for explanation on __get and __set. You can choose to store the data in a table called variables (fields : key,value). Or you could store it in memcache. The best solution would be to use a combination of both.

0

精彩评论

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