php into css ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ZoeF
Forum Newbie
Posts: 2
Joined: Fri May 12, 2006 5:56 pm

php into css ?

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: php into css ?

Post 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
Last edited by aerodromoi on Sat May 13, 2006 3:47 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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'] . '; }';
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ZoeF
Forum Newbie
Posts: 2
Joined: Fri May 12, 2006 5:56 pm

Post 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.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post 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.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post 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
Post Reply