I am trying to use a form to set colors in a table in my database, then call those colors into my stylesheet (style.php)
Defining the color variables manually works:
<?php
header("Content-type: text/css");
$color1 = '#cc0000';
?>
#thisdiv { background-color: <?php echo $color1; ?>; }
But this does not:
<?php
header("Content-type: text/css");
$getSettings = mysqli_query($db, "SELECT * FROM settings WHERE setting_id = 1");
$setting = mysqli_fetch_assoc($getSettings);
$color1 = $setting['setting_color1'];
?>
#thisdiv { background-color: <?php echo $color1; ?>; }
My 'settings' table looks like this:
setting_id | setting_color1 | setting_color2 | setting_color3
1 | #cc0000 | #000000 | #ffffff
How can I get this working properly? Maybe I am doing something wrong, or maybe I am just overlooking something stupid. Any help is appreciated.
<?php
header("Content-type: text/css");
$getSettings = mysqli_query($db, "SELECT * FROM settings WHERE setting_id = 1");
// --------------------------^ and where is that defined?
$setting = mysqli_fetch_assoc($getSettings);
$color1 = $setting['setting_color1'];
?>
#thisdiv { background-color: <?php echo $color1; ?>; }
You should always verify if your query ran successfully, isn't that hard.
<?php
if($result = mysqli_query($db, 'SELECT * FROM table') != false){
var_dump(mysqli_fetch_assoc($result));
// hurray, query ran successfully.
// Now execute the code that sets the variable.
} else {
// On fail, perhaps load from cache?
}
?>
Secondly, your DB structure is wrong in my opinion. Try something like the following.
settings:
id (primary key)| key (varchar)| value (varchar)| type (varchar)| decryption (text) | theme (int)
1 mycolor AABBCC hex Background of we.. 1
$sql = 'SELECT key, value, type FROM settings WHERE theme = 1'; // etc..
This way, you have much more control over your 'theme'. Then you can while-fetch the results and set: $arr[$row['key']] = $row['value']
.