Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Css

         

nicknick

6:19 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



/* CSS Document */
<?php

include "dbaconnect.php" ;

$sql = 'SELECT * FROM `Css` LIMIT 0, 30 ';
$result = mysql_query($sql);
while($rows = mysql_fetch_array( $result ))
{
Echo "$rows";
if($rows == 0)
{
echo "No entries 1 ";
}
else
{
while($row = mysql_fetch_array( $result ))
{

$bodyfont=$row["bodyfont"];
$bodycolor=$row["bodycolor"];
$h1font=$row["h1font"];
$h1color=$row["h1color"];
$main1width=$row["main1width"];
$mainback=$row["mainback"];
$mainheight=$row["mainheight"];
}
}
}
?>

body
{
color: <? $bodycolor ; ?>
font: <? $bodyfont ; ?>
}

h1
{
color: <? $h1color ; ?>
font: <? $h1font ; ?>
}

.main4
{width:<? $main1width ; ?> ;
border-top:1px solid #878787;
Border-right:1px solid #878787;
Border-bottom:1px solid #878787;
Border-left:1px solid #878787;
height: <? mainheight ; ?>;
background-color: <? $mainback ; ?>
}

I have a question :

1. do you save it as a .css or .php file ?
2. from a brief glance does it look like it will work or not ?

cameraman

7:04 pm on Aug 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From a brief glance it looks like it will work.
I would try saving it as a php file first; if you save it as a css you would need to tell the server to pass it through the php preprocessor.

I could see how this would be handy to support user-specific style sheets, but out of curiosity, what are you doing with it? If you're doing some sort of administrative style manager script it might be better to ultimately write this out as a static file.

tatorface

8:27 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



Your dynamic definitions of the colors:
<? $h1color ; ?>
need to have an echo in front of them:
<? echo $h1color ; ?>

Also, I am sure you know this but wanted to point it out. This will always return the exact same colors, given that there is a row in the table `Css`. You have no qualifier on your SQL query defining which row to use to specify the colors. So what you will get (given your table has over 30 rows) is a LONG list of CSS rules, each while iteration writing another set. I could be wrong, but ultimately the browser will end up using the last one written, which will make your page be the same every time.

I assume this is for users to change a color motif on your site. What I would do is add a color or name as a column to the table (for identification purposes). This way, you can use the name on your color change link on your site like this:
http://example.com/change_color.php?color=brown

Add this to the php file:

$color = $_GET['color'];

Change your sql query to something like this:
$sql = 'SELECT * FROM `Css` where color= '$color' LIMIT 0, 30 ';

This is how I would do what I think you are going for...

[edited by: dreamcatcher at 6:28 am (utc) on Aug. 16, 2008]
[edit reason] use example.com. Thanks. [/edit]

MattAU

10:07 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



Also, I am sure you know this but wanted to point it out. This will always return the exact same colors, given that there is a row in the table `Css`. You have no qualifier on your SQL query defining which row to use to specify the colors. So what you will get (given your table has over 30 rows) is a LONG list of CSS rules, each while iteration writing another set. I could be wrong, but ultimately the browser will end up using the last one written, which will make your page be the same every time.

You're right that given a database with more than 30 rows the colours will always be the same, but only the last row is being displayed so the CSS won't have multiple sets of rules.

Still, using a selector is important to get the required outcome. And is there a reason to get 30 rows when you only need one? Or did you copy the query from phpMyAdmin? :)