1

Currently, I am working on PHP codeignitor framework. I have some properties suppose like color,fonts etc. saved in my database for one of fields. All I want to do is, I have some classes in the css file with default values. Like ex. I have a css class as below:

     .text_box{
           color: pink;
           text-decoration: none;
           background-color: transparent !important;
           transition: all 0.2s ease-in-out 0s;
       }
       .text_box:hover{
             color: blue;
             text-decoration: none;
             background-color: transparent !important;
             transition: all 0.2s ease-in-out 0s;
        }

In my database, I have set color property as yellow for text box. I want to change it dynamically. means when I change that color from input field, it should automatically get converts into the color I want in css property.

I dont know exactly whether my question is correct or not. I have goggled many links but didn't get the relevant solution to my scenario.

Using PHP how can create such dynamic changes.

Thank you.

2
  • You say you want to use PHP to do assign the color, but PHP can only assign before the page is loaded. Is that what you want or do you want to change the color without reloading the page? Commented Mar 3, 2017 at 10:50
  • add your desired PHP code,(means where to add this) Commented Mar 3, 2017 at 14:58

5 Answers 5

5

You can do it using jQuery.

var color = "value from DB";
$('.text_box').css({ 'color' : color, });

or in PHP:

<?php
$color = 'value from DB'; //you have to get the value from db
?>
<style>
.text_box { 
color: <?php echo $color; ?>;
}
</style>

Hope this works.

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

3 Comments

Hi Vimal, Thanks for you reply. Actually, I am working on SaaS based application project. And I have 4 menuns in header section. So I want to make different css file. Is it any other way where I can put my css code and handle that as per my php code ?
@Joe-i think the best way to control css dynamically is the javascript.
happy to hear that..happy coding:-)
0

You can check Smarty

Basically, you read the values from your database and then you assign them to the template and then you will get dynamically loaded classes.

Comments

0

Php or javascript won't change your css file directly, but what you want to do is use Php or javascript to dynamically assign an id (#) to your HTML element that has the associated css you want.

#pink.text_box{
       color: pink;  //...
}
#blue.text_box{
       color: blue;  //...
}

And then use your javascript or php code to assign the id to the element you need

Comments

0

Check if you can use color picker tool. This is the best solution I think

http://www.dynamicdrive.com/dynamicindex11/yuicolorpicker/

Comments

0

Try using multiple css files like

css_red.css css_blue.css css_green.css with different default colors.

Then in php echo your stylesheet <link href="<?php echo "css_yourcolor";?>.css" >

1 Comment

This is not dynamically. This is just a more complex hardcoded solution. You can't have a stylesheet for each available color.

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.