views:

73

answers:

2

Hi guys. I'm looking to create an options table in my db that makes every record a system option, so I can work with a little number of fields.

My db has the following structure:

  • 3 columns named id, name, and value

The following data is inserted as an example:

+--+-----------+--------------------------+
|id|name       |value                     |
+--+-----------+--------------------------+
| 1|uri        |www.example.com           |
| 2|sitename   |Working it out            |
| 3|base_folder|/folder1/folder2/         |
| 4|slogan     |Just a slogan for the site|
+--+-----------+--------------------------+

That way I can include a large number of customizable system options very easily.

The problem is that I don't know how to retrieve them. How do I get the value of uri and store it as a var? And better yet, how do I get, for exmaple, values of id 1 and 4 only without making a query each time? (I assume multiple queries are useless and a pretty ugly method.)

I know the question is pretty basic but I'm lost here. I'd really appreciate your answer!

A: 

Here's something to get you started. No error checking or other niceties included. I assume your db name is $db_name and the table you describe above is called 'site_params'.

To get a single parameter from your table...

// select your db
mysql_select_db($db_name);

// get the uri setting
$query = "SELECT value FROM site_params WHERE name='uri'";
$result = mysql_query( $query );

If there aren't too many settings, you could just get all of them at once...

// get all the settings
$query = "SELECT * from site_params";
$result = mysql_query( $query );

Or get some of them...

// get uri and sitename
$query = "SELECT * from site_params where name in ('uri', 'sitename') ";
$result = mysql_query( $query );

You can receive the result as an array called $site_info by adding...

$site_info = mysql_fetch_array($result, MYSQL_ASSOC);

Clarification: this puts a single row in $site-info. Call it repeatedly to get multiple rows of the result.

To change one of the parameters...

// set new sitename
$newsitename = 'http://mynewsite.com';
$query = "UPDATE site_params SET value='" . $newsitename . "' where name='sitename'";
$result = mysql_query( $query );

Is that what you're looking for? Go to http://php.net for all the info you need. For example:

http://php.net/manual/en/function.mysql-query.php

Greg
Great! Thanks!I guess I was on the right path.I had a code similar to that.However i don't know how to get the value for 1 of the options. If i do this<?phpwhile($row = mysql_fetch_array($result)) { $thumbnails_folder = $row["thumbnails_folder"];}?>I get nothing because the row doesn't exist.Sorry, i probably didn't explain this well.What i want to avoid is having a query for each parameter.I've been using while {} around the site, for example, to list users. I feel I need to use another function here, but i can't figure out which one.Thanks again!
Nacho
I'm not sure I understand what you're getting at (if the row doesn't exist then your query didn't pull it in), but it sounds like you might think that your while loop is doing multiple queries. But it isn't. The only query is happening when you call mysql_query.You either do a query that gets a single parameter (and multiple queries for multiple parameters) OR you do a single query for multiple parameters and then retrieve them from the result a row at a time.
Greg
Sorry, looks like I couldn't explain it well.I have, as in the example above, an entry for every site option (instead of a single entry table with lots of rows, where each one could be an option).What i want to do, is take the value of the 3rd row for EACH of the entries and store each one as a var (or an array).So, if I do and echo of the site parameter 'base_folder' (which is the entry with ID #3), it gives me the value '/folder1/folder2/'.I need that for EVERY entry in the db, and i think it can be done without a query for each one. Can it?Thanks for answering :D
Nacho
A: 

$values = array(); $q = "SELECT * FROM table"; $resu = mysql_query($q);

while ($row = mysql_fetch_array($resu)) { $values[$row['name']] = $row['value']; }

usage: echo $value['variable_name'];

Bruno