Page 1 of 1
php into css ?
Posted: Fri May 12, 2006 6:01 pm
by ZoeF
Me and my brother lookd add all kind of codes from phpbb2 to vwar and we tryed 2 find how they get the css and php combined but we couldn't find it. We only got more confused :'( .
Is there anyone that knows if it is possible 2 put php into css or if there is a way 2 work around it? If so please help me and my bro with this. I would realy appreciat the help.
Greetz ZoeF
Posted: Fri May 12, 2006 6:08 pm
by RobertGonzalez
phpBB stores style attributes in a database. They also place their style sheet in the header html of the output pages. So what they do is pretty simple: Grab the stored style information from the database and display it as part of the html output.
Re: php into css ?
Posted: Sat May 13, 2006 1:59 am
by aerodromoi
ZoeF wrote:
Is there anyone that knows if it is possible 2 put php into css or if there is a way 2 work around it?
PHP is a scripting language, which means scripts are parsed by the server before any output is sent.
css is a a stylesheet language which defines the layout of a page on the client-side. This said, you cannot "put php into css" but you can include css information on a page via php.
aerodromoi
Posted: Sat May 13, 2006 2:10 am
by s.dot
This will work:
Code: Select all
<?php
$textColor = '#FFFFFF';
$backGround = '#000000';
$fontSize = '12pt';
?>
<html>
<head>
<title>My Page</title>
<style type="text/css">
body {
background-color: <?php echo $backGround; ?>;
font-size: <?php echo $fontSize; ?>;
color: <?php echo $textColor; ?>;
}
</style>
</head>
<body>My Page</body>
</html>
Posted: Sat May 13, 2006 3:40 am
by timvw
It's perfectly possible to generate CSS (or whatever you want) instead of the usual HTML..
The only thing you have to keep in mind is to output the correct content-type header...
Eg: mystyle.css.php
Code: Select all
<?php
session_start();
header('Content-type: text/css');
echo 'body { background-color: ' . $_SESSION['background']['color'] . '; }';
?>
Posted: Sat May 13, 2006 9:01 am
by RobertGonzalez
And, using timvw's suggestion, you can offer dynamic styling through querystrings...
Code: Select all
<link rel="stylesheet" href="mystyle.css.php?userscheme=blue" type="text/css" />
Then in your PHP style sheet you can add some stuff...
Code: Select all
<?php
if (isset($_GET['userscheme']))
$style_data = get_style_data($_GET['userscheme']); // assumes a user defined function to get style data
header('Content-type: text/css');
echo 'body { background-color: ' . $style_data['background-color'] . '; }';
?>
This is untested, but I think I saw something like this on Sitepoint or A List Apart or something.
Posted: Sun May 14, 2006 2:43 am
by ZoeF
Code: Select all
<?php
include "config.php";
$sql = mysql_query("SELECT * From ga_styles WHERE id = '".$sid."'");
$style = mysql_fetch_assoc($sql);
header('Content-type: text/css');
// body
echo '
body { background-color: '.$style["bbcolor"].';
font-family: '.$style["font"].';
color: '.$style["tcolor"].';
font-size: 12px;
margin-top: 0px;
margin-bottom: 0px; }';
// link
echo '
a:link { color: '.$style["alink"].';
text-decoration: '.$style["adeco"].';}';
// hover
echo '
a:hover { color: '.$style["ahover"].';
background-color: '.$style["hbcolor"].';
text-decoration: '.$style["ahdeco"].';}';
......
Just 2 give you a idee. Tnx for you help guyz. I wil sertenly visist this great group of people more.
Thumbs up for the quick responses.
Posted: Sun May 14, 2006 9:14 am
by someberry
timvw wrote:It's perfectly possible to generate CSS (or whatever you want) instead of the usual HTML..
The only thing you have to keep in mind is to output the correct content-type header...
Eg: mystyle.css.php
Code: Select all
<?php
session_start();
header('Content-type: text/css');
echo 'body { background-color: ' . $_SESSION['background']['color'] . '; }';
?>
Personally I would tell Apache to parse .css files as PHP, it looks better than .css.php. Also it obscures that you are using PHP in your CSS files.
Posted: Sun May 14, 2006 9:44 am
by aerodromoi
someberry wrote:
Personally I would tell Apache to parse .css files as PHP, it looks better than .css.php. Also it obscures that you are using PHP in your CSS files.
In my book, it depends on the ratio between php-generated css files and "normal" css files.
Anyway, if you're on a shared account, there's always the directory trick (naming a directory xyz.css
and putting the php file inside as index.php).
aerodromoi