开发者

Store data in txt/xml file, read/write to database?

My wordpress theme accepts skin files. These skin files all install into my main theme folder via a zip uploader that's part of my theme.

Each skin has a set of custom color codes (4 in all) that are stored in the wordpress options table like so...

Assume the skin name is "halloween"...These are the values in my options.php for one of my skin values...

halloween_color1 = 000000
halloween_color2 = ff0000
halloween_color3 = 777777
halloween_color4 = 333333

So I just need a means to store these values inside of each new skin's folder (the one that I send to people who use my theme) so that when they install the skin (via a simple zip extractor upload) I can place code into my zip extractor to write th开发者_如何学Ce skin's custom color values to the database.

I'm assuming a simple, colors.txt or colors.xml file will suffice.

How should I store the data in the text file in order to easily parse it and write it to the database? Name/value pairs or XML?

<skin>
<color name="halloween_color1" value="000000" />
<color name="halloween_color2" value="000000" />
<color name="halloween_color3" value="000000" />
<color name="halloween_color1" value="000000" />
</skin>


There are several options. Two that PHP can read natively are ini and CSV.

INI example:

[skin]
halloween_color1 = 000000
halloween_color2 = ff0000
halloween_color3 = 777777
halloween_color4 = 333333

CSV example:

halloween_color1;000000
halloween_color2;ff0000
halloween_color3;777777
halloween_color4;333333

For me personally, YAML has become the favourite format for human-readable configuration files.

YAML example:

skin:
 halloween_colors:
   - 000000
   - ff0000
   - 777777
   - 333333

its advantages in my view are:

  • Parsing is very strict; it will exit immediately and throw an error if it doesn't like the file's structure

  • It supports nested data structures, the building of associative arrays, typing, and lists (Useful e.g. if you want to add a fifth halloween colour)

But, it needs a third party library. Whether that is justified, you have to decide. See a list of PHP parser libraries here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜