Jump to content

Site Options Table


rawrtiger

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/262436-site-options-table/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/262436-site-options-table/#findComment-1344936
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/262436-site-options-table/#findComment-1344938
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.