0

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.

7
  • Try inspecting the code and verify what is in there. You can do this by pressing CTRL+U Commented Mar 17, 2016 at 17:25
  • How this code is supposed to work without a database connection? Commented Mar 17, 2016 at 17:26
  • @PhiterFernandes there is nothing in background-color - it's just empty. @YourCommonSense $db connection is defined elsewhere. It was tested and is working properly. I figured that would be assumed, sorry. The query was checked (as per a comment below) and is working also. Commented Mar 17, 2016 at 17:42
  • Run a var_dump($setting) before you echo it and check the values. Commented Mar 17, 2016 at 17:51
  • A db connection doesn't work if you define it elsewhere and have not a single reference to it in your code Commented Mar 17, 2016 at 18:01

1 Answer 1

1
<?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'].

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the reply. I have $db defined elsewhere, but the connection was tested and is working properly. I checked my query with your code above and it was successful as well.
If that's the case, try the updated code it would show debug output that is actually quite important to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.