php into css ?
Moderator: General Moderators
php into css ?
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
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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
Re: php into css ?
PHP is a scripting language, which means scripts are parsed by the server before any output is sent.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?
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.
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.
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
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'] . '; }';
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
And, using timvw's suggestion, you can offer dynamic styling through querystrings...
Then in your PHP style sheet you can add some stuff...
This is untested, but I think I saw something like this on Sitepoint or A List Apart or something.
Code: Select all
<link rel="stylesheet" href="mystyle.css.php?userscheme=blue" type="text/css" />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'] . '; }';
?>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"].';}';
......Thumbs up for the quick responses.
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.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'] . '; }'; ?>
- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
In my book, it depends on the ratio between php-generated css files and "normal" css files.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.
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