Page 1 of 1

css works with php?

Posted: Tue May 05, 2009 10:40 am
by naseemsadak
Hi,

I just want to know if css actually works with php? I want to add a simple background image to my website but what I do not understand is, do you have to save your script as .css or .php in order for both to work? I'm using an old version of php editor 2.2 to do php scripts.

Thanks,

Naseem

Re: css works with php?

Posted: Tue May 05, 2009 10:49 am
by Dale
Well CSS works on it's own, either in a seperate file or you can embed it on the page.

For example, if you put this in the <head> </head> section of your site it'll work fine.

Code: Select all

<style type="text/css">
body {
background: url('http://www.url.com/picture.gif');
}
</style>
That can be put in your PHP file if you have a <head> </head> tag.

Re: css works with php?

Posted: Tue May 05, 2009 11:00 am
by naseemsadak
Thanks allot. Well if there is any useful links on working with css and php, I'd appreciate if someone could provide me with website addresses.

Re: css works with php?

Posted: Tue May 05, 2009 11:13 am
by nwhiting
What do you mean by working with css and php?

Dynamically creating CSS styles using PHP?

If so take a look at this
http://www.barelyfitz.com/projects/csscolor/

Re: css works with php?

Posted: Tue May 05, 2009 11:46 am
by naseemsadak
Meaning working with css in php scripts. I've been searching the net for some info and from what I read, that for css you have to save your script as .css. I just tried creating a simple php script with css for background image but couldn't get it working. I know html and css work, just my confusion is php working with css.

Re: css works with php?

Posted: Tue May 05, 2009 1:11 pm
by naseemsadak

Code: Select all

<html>
<head>
<?php
header("Content-type: text/css");
?>
body
{
background: url(<?php echo('http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg');?>;
}
</head>
<body>
</body>
</html>
Now I get this error:-

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\testcss.php:3) in C:\xampp\htdocs\testcss.php on line 4
body { background: url(http://www.travelblog.org/Wallpaper/pix ... 0x1200.jpg; }

Tried my best but still stuck.

Re: css works with php?

Posted: Tue May 05, 2009 1:19 pm
by nwhiting
naseemsadak wrote:

Code: Select all

<html>
<head>
<?php
header("Content-type: text/css");
?>
body
{
background: url(<?php echo('http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg');?>;
}
</head>
<body>
</body>
</html>
This is completely wrong.....you do not use the header() for parsing css.
Please read what the php header does
http://php.net/header
And as far your content goes it is almost correct, you do not need to echo out the image location

Code: Select all

 
<html>
<head>
body
{
/* WORKS *
background: url('http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg');
 
/* ALSO WORKS */
background: url(<?php echo '"http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg"'; ?>);
}
</head>
<body>
</body>
</html>

Re: css works with php?

Posted: Tue May 05, 2009 1:30 pm
by naseemsadak
So would I need a style tag or do away with both style and header tags?

Re: css works with php?

Posted: Tue May 05, 2009 1:37 pm
by naseemsadak
I want css to work in php

Re: css works with php?

Posted: Tue May 05, 2009 1:53 pm
by pickle
I think you're misunderstanding what PHP does - a common misunderstanding.

PHP is a server side language. The browser never sees it. When you go to a URL that is a PHP file, the server executes the PHP file and that PHP file then outputs something. It can be an image, a csv, but most often it's an HTML file.

Your browser then receives that html code & treats it like it should - as html.

Any CSS you are using interacts with your html on your computer. PHP and CSS never interact, nor can they. One is server side, the other is client side.

Re: css works with php?

Posted: Tue May 05, 2009 1:58 pm
by naseemsadak
Thanks this was the answer I was looking for at first whether css and php work with each other and now I know.

Re: css works with php?

Posted: Tue May 05, 2009 2:06 pm
by Reviresco
Here's an example of using php with css. It uses an inline style, but it could just as easily be used in the style definitions in the head of the document.

Code: Select all

 
if($f->query_string == 'young_adults') { 
echo ' style="background-image:url(../images/backgrounds/bg_young_adult.jpg);"';
}
 
if($f->query_string == 'parents') { 
echo ' style="background-image:url(../images/backgrounds/bg_parenting.jpg); "';
}
 
if($f->query_string == 'motherhood') { 
echo ' style="background-image:url(../images/backgrounds/bg_motherhood.jpg);"';
}
 
if($f->query_string == 'aging') { 
echo ' style="background-image:url(../images/backgrounds/bg_aging.jpg);"';
}
 
Also, this is not written in the most efficient way, but it serves to illustrate. The query string could be in the URL, such as:

http://www.example.com?f=aging

or it could be submitted via a form.

Re: css works with php?

Posted: Tue May 05, 2009 2:51 pm
by Glycerine
I think what you mean is a sort of dynamic CSS -

There are some ways you can achieve this.
Instead of you having a seperate file .css you have inline CSS with the "script" block. This allows you to add CSS to the .php page...

File.php

Code: Select all

 
<?php
$header_color = "#AAFF00" //some sort of dynamic color creation...
?>
 
<html>
<head>
<style type="text/css">
 
.headerColor{
color: <?php echo $header_color; ?>;
}
 
</style>
</head>
<body>
 
<h1 class='headerColor'>My Header 1</h1>
 
...
 
</body
</html>
 

The second alterative is to create a php page of which sole serves css formatted data...

You can still use the header() command...

Code: Select all

<html>
<head>
<?php
/*
 
By right, you cannot send a header once you placed ANY character in the output buffer (the stuff to see),
in this code, you have already "sent" '<html>' and '<head>' as such, destroy the data you need to send out...
 
*/
ob_flush();
ob_end_clean()
header("Content-type: text/css");
?>
body
{
background: url(<?php echo('http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg');?>;
}
</head>
<body>
</body>
</html>

or finally - change where the header gets sent...

Code: Select all

 
<?php
header("Content-type: text/css");
 
echo ".headerColor{\ncolor: red;\n}\n";
 
 
then you referance this as you would any other css page...

Code: Select all

 
 
<style type="text/css">
            @import url('dynamic_css.php');
</style>
 

Re: css works with php?

Posted: Wed May 06, 2009 1:12 am
by naseemsadak
I found this article that has helped me http://www.daniweb.com/forums/thread160935.html#