- Today's scripting triumph
- June 22nd, 2010
I wanted to figure out a convenient way to load a bunch of configuration variables for a web page "content assembly" (my name for a section of a page - I would have used "section" except that might get confusing if the site had sections with multiple pages). For example, one of the assemblies is "Find Us" which contains options for displaying any or all of: the address, a map link, an embedded map and directions. Depending on the site's content & layout and the needs of the site owner, this could go on a "Contact Us" page, or maybe an "About Us" page. In any case, I want to be able to have these assemblies be modular so they're not permanently chained to any one page.
Each assembly will have a different set (and quanitity) of config variables, so I couldn't really create one MySQL table to hold the config vars for each assembly in separate columns, and I sure didn't want to create a separate config table for each assembly.
I decided to see if I could take the data from the config form and assemble it into a string, which could be stored in one column of a DB table, then be able to pull that string out and parse it into variable names with values.
I did it! And it's only about 12 lines (not counting the queries, just the string creation and parsing code). It takes the name and value of the appropriate form fields, pushes them into an array (each key/value pair is an element in the array) then implodes the array into a string which would then get inserted (or updated) into the database table.
When the string is pulled out of the table, the code explodes the string into an array, then loops through each element (key/value pair as a string) and explodes that into an array with two elements. The key name (  ) gets assigned to a variable, which is then assigned to a variable variable (making it a variable with the name of the key) and assigns the appropriate value (  ) to it.
It ends up "decompressing" the string into all the necessary config variables with appropriate names, ready to be used in the assembly.
This is starting to get fun!