css works with php?

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
naseemsadak
Forum Commoner
Posts: 28
Joined: Wed Apr 15, 2009 2:50 am

css works with php?

Post 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
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Re: css works with php?

Post 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.
naseemsadak
Forum Commoner
Posts: 28
Joined: Wed Apr 15, 2009 2:50 am

Re: css works with php?

Post 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.
nwhiting
Forum Newbie
Posts: 18
Joined: Sun May 03, 2009 8:30 pm

Re: css works with php?

Post 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/
naseemsadak
Forum Commoner
Posts: 28
Joined: Wed Apr 15, 2009 2:50 am

Re: css works with php?

Post 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.
naseemsadak
Forum Commoner
Posts: 28
Joined: Wed Apr 15, 2009 2:50 am

Re: css works with php?

Post 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.
Last edited by Benjamin on Tue May 05, 2009 1:20 pm, edited 1 time in total.
Reason: Changed code type from text to html.
nwhiting
Forum Newbie
Posts: 18
Joined: Sun May 03, 2009 8:30 pm

Re: css works with php?

Post 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>
Last edited by Benjamin on Tue May 05, 2009 1:20 pm, edited 1 time in total.
Reason: Changed code type from text to php.
naseemsadak
Forum Commoner
Posts: 28
Joined: Wed Apr 15, 2009 2:50 am

Re: css works with php?

Post by naseemsadak »

So would I need a style tag or do away with both style and header tags?
naseemsadak
Forum Commoner
Posts: 28
Joined: Wed Apr 15, 2009 2:50 am

Re: css works with php?

Post by naseemsadak »

I want css to work in php
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: css works with php?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
naseemsadak
Forum Commoner
Posts: 28
Joined: Wed Apr 15, 2009 2:50 am

Re: css works with php?

Post 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.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: css works with php?

Post 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.
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

Re: css works with php?

Post 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>
 
Last edited by Benjamin on Tue May 05, 2009 3:07 pm, edited 1 time in total.
Reason: Changed code tags to html, php
naseemsadak
Forum Commoner
Posts: 28
Joined: Wed Apr 15, 2009 2:50 am

Re: css works with php?

Post by naseemsadak »

I found this article that has helped me http://www.daniweb.com/forums/thread160935.html#
Post Reply