rawrtiger Posted May 12, 2012 Share Posted May 12, 2012 I'd like to have a table that has all my site options in it (eg. Site Name, Site URL, etc.) and then an easy way to define variables from it in PHP. I currently have the table structured like so: ID _|_ Setting _|_ Value 1 | setting1 | a 2 | setting2 | b 3 | setting3 | c 4 | setting4 | d I'd like a file that will select each value and define it as a variable in PHP so I can use the variables in my other files. This is what I'm doing for now but it's not a very good way to do it: $result = mysql_query("SELECT * FROM config WHERE site_option = 'setting1'"); while($row = mysql_fetch_array($result)) { $pagetitle = $row['value']; } $result = mysql_query("SELECT * FROM config WHERE site_option = 'setting2'"); while($row = mysql_fetch_array($result)) { $siteurl = $row['value']; } What would be a better way to do it? Quote Link to comment Share on other sites More sharing options...
trq Posted May 12, 2012 Share Posted May 12, 2012 Just store them all in an array. <?php $config = array(); if ($result = mysql_query("SELECT Setting, Value FROM config")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { $config[$row['Setting']] = $row['Value']; } } } Now that $config array holds all your settings. Quote Link to comment Share on other sites More sharing options...
rawrtiger Posted May 12, 2012 Author Share Posted May 12, 2012 Just store them all in an array. <?php $config = array(); if ($result = mysql_query("SELECT Setting, Value FROM config")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { $config[$row['Setting']] = $row['Value']; } } } Now that $config array holds all your settings. Thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.