I have a table called "system_config". The structure is as follows: id | config_name | config_value So, an entry might be: 1 | language | english I am having trouble getting these values out of the DB in a particular way. I am trying to get where I can do something like: echo $config["language"]; PHP: and see the word "english" on the page. Any suggestions???
Have you tried hiring someone to do this for you? If not, what have you got so far? Where are you stuck?
Well I have this: $systemconfig = mysql_query("SELECT * FROM system_config"); while ($configs = mysql_fetch_array($systemconfig)) { echo $configs["config_name"].": ".$configs["config_value"]."<br />"; } PHP: ...But this forces me to just echo all the values in the while loop. I need to be able to place these values at different places throughout the page. What I'm thinking now is that I need to use class objects somehow so that I can do something like: echo $config->language; PHP: ...to echo the value "English" on the page. I really need some help trying to get this going.
How do you know which row to use and when? Presumably $configs['config_name'] will contain different values and not just "English" depending on what row you are working with. Also, in your first post you wanted an array ($config['language']) and now you've stated you want an object ($config->language). Can you provide more clarification on exactly what you are trying to output to the browser?
I'm trying to figure which method would work best. Because $configs['config_name'] will contain different values, that's why I now believe an object would be better. The only thing I'm trying to output to the browser is the config_value. That's why I think I can do something like $config->language to get "English", or $config->country to get "US", etc. I am just lost when working with objects.
Ok, this will work like you initially posted, however, if you have 2 records in your database that have the same config_name (i.e. 'language', the last one pulled will be the value you get.) $config=array(); // array to hold all config_name values $systemconfig = mysql_query("SELECT * FROM system_config;"); while ($configs = mysql_fetch_array($systemconfig)) $config[$configs['config_name']]=$configs['config_value']; // puts config values into $config where config_name is array key echo $config['language']; PHP:
Thank you for your help, but like I mentioned, this would only work in the while loop. That's pretty much identical to the code I have. I think it's going to have to be done as an object from everything I can tell (but I could be wrong)
I don't understand what you mean by "would only work in the while loop". Since your extracting an unknown amount of rows from a query, you are going to have to use a while loop no matter what. All the data in the $config array will be accessible outside of the while loop.