Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I apply style values from database 1

Status
Not open for further replies.

Microbe

Programmer
Oct 16, 2000
607
AU
Hey folks,

I have a CSS based menu system for a browser based application I am developing for volunteer bushfire brigades in Australia.

It pulls the styles from css files (obviously) but I would like to make it configurable and store values in a mySQL database.

That's the easy part, but then how do I get the values back into the style sheet?

Code:
background: <?php echo("#FF9900"); ?>;
won't work because it isn't a php page.

but that's the sort of thing I want to do.

The only other thing I can think of is to have a PHP include that adds the styles to the page rather than as a linked style sheet.

Is there a better way to do it?

Steve Davis

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
Yes - simply make your CSS files PHP files, so instead of this:

Code:
<link rel="stylesheet" type="text/css" href="someFile.css" />

do this:

Code:
<link rel="stylesheet" type="text/css" href="someFile.[!]php[/!]" />

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Doh! Not having worked a lot with style sheets, I had no idea it was so simple.

Thanks so much!

Steve Davis

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
Dan,

Another question if you don't mind.

Firstly, it works. I even have the page building on the fly from whatever fields are in the database, the colour values can be updated and it works perfectly...almost.

If I embed the styles directly in the page with the PHP code to get the db data at the top it works. i.e.
Code:
<?php SQL STATEMENT POPULATING ARRAY FROM RECORDSET ?>
...
other head stuff
...
<style>
border: 1px solid <?php echo($arrValues['borderColour']["value"]); ?>;
</style>
<body>
...

However if I link to the style sheet with the db get at the top, even if it is a PHP script it doesn't work - the styles are completely lost.
Code:
<head>
<link rel="stylesheet" type="text/css" href="includes/menu.php" />
</head>

AND THEN IN MENU.PHP

<?php SQL STATEMENT POPULATING ARRAY FROM RECORDSET ?>

border: 1px solid <?php echo($arrValues['borderColour']["value"]); ?>;

I have tried a few variations on the theme including putting the SQL in the document and linking to the stylesheet, trying to include the SQL from a separate document into the style sheet before calling it in my script.

What I have ended up with that works, but it is a kludge in my opinion, is to have the external PHP script get the recordset then echo the style information. Then include that PHP script so the styles end up being written at the top of the page.

But something about this gives me the creeps...I am sure it can be done more efficiently.

Can't find anything relating to this anywhere, so hope you might be able to point the way...again...

Thanks so much in advance.

Oh...and the demo scripts on your website are just wonderful...congratulations.

Steve Davis

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
well, that depends.

If I do what I said kludged around it i.e. use require(menu.php) it works because really all that is doing is getting the data, then echoing a long string (snipped here for brevity)
Code:
echo("<style> border: 1px solid" .($arrValues['borderColour']["value"]). "</style>");
and dropping it at the top of the page. The style data is visible in the HTML source.

However if you do the standard stylesheet link e.g. <link href="menu/php" rel="stylesheet"> it doesn't show all the styles in the HTML source. Which is what I am really after.

But as I said, I can't quite get it to work like that.

Since it _does_ work now, I fear becoming pedantic about it, but I prefer to learn something so I can code properly the first time. Trying to avoid a "who the hell coded this?" from someone else down the track :)

Steve Davis

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
Your example of linking leaves out the most important attribute in the link tag. The type will tell your browser what type the document is and what engine to use to parse it. Without it, browser will believe it is a php page and not a css stylesheet. Try this:
Code:
<link href="menu/php" rel="stylesheet" [!]type="text/css"[/!] />
Incidentally, that is what Dan told you to do in the first place.
 
incidentally...I actually did, but had snipped that from the example.

Steve Davis

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
I don't understand why this is getting this complex. Surely all you need is a PHP file, made up of mostly CSS, but small amounts of PHP, something like:

Code:
<?
   // Code to populate $arrValues goes here
?>

#someRule1 {
   someProp1: someVaue;
   someProp2: someVaue;
}

#someRule2 {
   someProp1: someVaue;
   someProp2: someVaue;
}

#someRule3 {
   someProp1: someVaue;
   border: 1px solid <?=$arrValues['borderColour']['value']?>;
}

and then link this as normal.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Note: If, when you view the menu.php file, you don't see valid CSS, then you should address that issue. If you don't see valid CSS, then the browser certainly isn't going to.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
well...guess what was the fix.

Take your code sample and put <style> </style> around the CSS.

I guess that's where the frustration was...that the info given didn't actually work when I expected it to. But being tenacious and testing lots of things I actually discovered it for myself.

Thanks for your help Dan. I got there...

Steve Davis

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
Also, for firefox, make sure you set the content-type of the file using php.

In my asp web application, I wanted active content in my .css files, so I just changed the engine to run css files through the asp parser dll. I had to put

<%Response.ContentType = "text/css"%>

in the head of each of my css files to make Firefox work (IE does its typical guessing job). But I consider that a small price to pay to have active css.

I like using color constants like <%=LOGOCOLOR%>. In the source css I think it is superior to, from a programming perspective, putting whatever the literal logo color is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top