Jump to content


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


PHP & Styles & CSS

Recommended Posts

G'day Guys,

I have a page where a user selects the colours for different Fonts and Tables backgrounds. What I need to do is write this data to either a database or the CSS file.

What would be the best method of doing this in your own opinion(s).

*(I know how to do the database stuff, but unsure how to utilise the info when retireved. (e.g. in my page or can a CSS use PHP)

*(I'm not real flash with working with files but sure I could sort it out, should I rewrite the CSS or just 'edit' the required bits....I don't know how to 'edit' a file using php)

Appreciate any help, understand if none given

Share this post

Link to post
Share on other sites
Hej Humpty,
Interesting problem you got there,

A simple way would be if you had a form that took in the desired input on what the fonts and tables looks like and then wrote to a preexisting stylesheet and a page that linked to that css.

First write a stylesheet stub and set the permissions so you can write to it

prewrite all of the information that is static, then have the script append all the dynamic information.

$file ="DynamicStyleSheet.css"
$fh = fopen($file,'a') or die(echo 'File ($file) does not exist';)
fputs($fh, $divContent);

(Quick and dirty, need much more comments/error handling here)

where $divContent is a string containing the css code needed to generate the look for a certain div id or class.

This should works but because it it doesn't overwrite the style sheet will get quite long after a while. I'll look into a better way later today.


Share this post

Link to post
Share on other sites

I've laid out my CSS how I want it, (things that might be added or changed will be appended at the bottom...and fully commented so i can refer to it later)

I am about to use a PHP page and code to:
1) Open the file
2) Write the ENTIRE thing again (over the top of the existing data, completely losing it)
3) Save the file.

Sound like a good plan? or any better ideas at this point in time?

Share this post

Link to post
Share on other sites
Well I just wasted 30 minutes of my life sorting that out.

When someone opens the page to change colours and doesn't select some colours my CSS will be missing colours.

I think I need a way to either:
1) Load the colours as required from the CSS file
(so that they are listed as default colours in the text boxes that correspond wth the colour pickers)

2) Only edit parts of the CSS file where appropriate
(but I don't know how to edit only PARTS of a file)

How do I edit only parts of a file, (e.g. a specific line number in a file of lets say 10 lines?)

I know this was in the manual but it didn't make sense to me.

Share this post

Link to post
Share on other sites
i would encourage you NOT to actually write the contents that the user pics to a CSS page. as soon as you get a user that's prone to changing things up over and over again or playing with different color combinations, you open yourself up to a mess of overhead trying to overwrite the file every time. here's what i'd suggest:

1) create a CSS file with all the static classes and ids (static being the ones that ARE NOT changeable)

2) create a header file that can be included on every page of your site that declares the dynamic classes and ids within it. rather than hard-coding hex or rgb values into the page, you could use PHP to pull the selected information from a DB and echo it within that style declaration.

3) populate your DB with default values and create an interface where the user can change those values at will.

hope this makes sense to you.

Share this post

Link to post
Share on other sites


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.