Jump to content

Archived

This topic is now archived and is closed to further replies.

steelmanronald06

CSS and PHP

Recommended Posts

How would I do something like this in CSS with PHP?  It doesn't work, but surely you can understand what I want:

[code]#topnav  {
background:url($path/includes/teal.gif) repeat-x;
padding-top:5px;
color:#fff;
}
[/code]

Share this post


Link to post
Share on other sites
ok try

[code]
#topnav  {
background:url(<?php$path?>/includes/teal.gif) repeat-x;
padding-top:5px;
color:#fff;
}
[/code]

Share this post


Link to post
Share on other sites
have u defined the $path???

try this i know wat went wrong there

[code]
<?php
Echo("
#topnav  {
background:url($path/includes/teal.gif) repeat-x;
padding-top:5px;
color:#fff;
}
");
?>
[/code]

Share this post


Link to post
Share on other sites
^ Yea that would work as well but I already had this type so here you go...

[code]
#topnav  {
background:url(<?php echo $path; ?>/includes/teal.gif) repeat-x;
padding-top:5px;
color:#fff;
}[/code]

Hope that works for you.

Tom

Share this post


Link to post
Share on other sites
No steelman is correct. It does not work that way...

You have to define the path not set a variable. So somewhere you have to do this.
[code=php:0]
define(path, 'http://www.yoursite.com', true);
[/code]

now you can do this in the css file. Once again you have to change the extentsion to php

[code]
#topnav  {
background:url(<?php path ?>/includes/teal.gif) repeat-x;
padding-top:5px;
color:#fff;
}[/code]

I tested this and it works fine..

Sorry that i said that you were wrong before testing it my self.

Tom

Share this post


Link to post
Share on other sites
You could use sessions.

index.php:
[code]
<?php
session_start();
$_SESSION['path'] = "/home/you/public_html/something";
echo <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>Test site</title>
<link rel="stylesheet" type="text/css" href="stylesheet.php />
</head>
<body>

<!-- some html here -->

</body>
</html>

EOF;
?>[/code]

stylesheet.php:
[code]<?php
session_start();
header("Content-type: text/css");
echo <<<EOF
#topnav {
background:url({$_SESSION['path']}/includes/teal.gif) repeat-x;
padding-top:5px;
color:#fff;
}

EOF;
?>[/code]

Share this post


Link to post
Share on other sites
yea that is what I did and it works fine.  Once again I am sorry for telling you that you were doing something wrong before testing it my self..

Good luck,
Tom

Share this post


Link to post
Share on other sites
Okay, just gave this a test and it doesn't work :(

index.php
[code]
<?php
$path = ".";

define(root, '.', true);

include($path.'/includes/overall_header.inc');

echo '<h1>Blah</h1>';

include($path.'/includes/overall_footer.inc');

?>
[/code]


style1.css
[code]
#topnav  {
background:url(<?php root ?>/includes/teal.gif) repeat-x;
padding-top:5px;
color:#fff;
}
[/code]

I know I have all the paths defined correctly :(

Share this post


Link to post
Share on other sites
I don't know what to tell you. It worked fine for me. The only difference was that I defined the entire url. May be you should try that instead of just ( . )


Good Luck,
Tom

Share this post


Link to post
Share on other sites
I think your file style1.css should be renamed to style1.php (so it can execute the php code).

Share this post


Link to post
Share on other sites
For starters the first argument [i]define[/i] excepts is a string, not a constant.

[code=php:0]
define("root",".");
[/code]

However... constant or not, this has nothing to do with your problem. This...

[code]
#topnav  {
background:url(<?php echo $path; ?>/includes/teal.gif) repeat-x;
padding-top:5px;
color:#fff;
}
[/code]

is perfectly legitimate code if your css file has the .php extension. You may also need to define the mime type however. Place this at the top.

[code=php:0]
<?php header("Content-type: text/css"); ?>
[/code]

Share this post


Link to post
Share on other sites
okay, now I can't even get they stylesheet to call. Here is what I have:

index.php
[code]
<?php

ob_start();
define(root, '.', true);

$path = ".";


//require the headers
require_once('includes/headers.php');

// require the database connection
require_once('includes/db.php');

//Require functions.php
require_once('includes/functions.php');

// require the top layout
require_once('includes/top.php');
[/code]

top.php
[code]
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>

<!--BEGIN META-->

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<meta name="Keywords" content="free linux tutorials, free apache tutorials, free php and mysql tutorials, live forums, free classes, free forums, programming help, php help, mysql help, apache help, free php help, php tutorials" />

<meta name="Description" content="LAMPGeekz offers free news, tutorials, classes, and live forums in Linux, Apache, MySQL, PHP, and more! Get all of your programming help here!" />

<!-- Begin Stylesheets -->
<?php
echo "
<link rel='stylesheet' type='text/css'  href=$path.'/includes/style1.php' />
<link rel='stylesheet' type='text/css'  href=$path.'/includes/style1.php' media='print' />
";
?>
[/code]

I renamed the stylesheets as style1.php and style2.php just as said.

style1.php
[code]
/* CSS Document */

body  {
font-family:Verdana, arial, times, helvetica, arial, sans-serif;
margin:0;
padding:0;
font-size:78%;
}

a {
text-decoration:none;
}

a:hover {
text-decoration:underline;
}

code {
font-family:Verdana, arial, times, helvetica, arial, sans-serif;
font-size:76%;
}

dt {
font-weight:bold;
}

pre {
font-family:Verdana, arial, times, helvetica, arial, sans-serif;
font-size:76%;
}

/************************    HEADER TEXT  ************/
#title  {
font-size:1.9em;
font-family:Georgia, verdana, arial, helvetica, arial, sans-serif;
padding-left:100px;
font-weight:normal;
margin-top:10px;;
}
/**********************    NAVIGATION IN TOP-RIGHT CORNER  *****/
#cornernav  {
float:right;
margin-top:5px;
margin-right:5px;
}

#cornernav a {
color:#686868;
padding-right:20px;
}
/***************************  INLINE NAVIGATION  WRAPPER    *****/
#navwrap  {
position:relative;
padding-top:1px;
padding-bottom:1px;
border-top:3px solid #888;
border-bottom:2px solid #888;
}
/*****************************  INLINE NAVIGATION  *********/

#topnav  {
background:url(<?php root ?>/includes/teal.gif) repeat-x;
padding-top:5px;
color:#fff;
}



#topnav ul
{
height:2.4em;
line-height:2.4em;
list-style: none;
padding:0;
margin:0;
}

#topnav li
{
float: left;
}

#topnav li a
{
line-height:2.4em;
float:left;
width:80px;
display: block;
color: #fff;
text-align: center;
}
/****************************    INPUT BUTTONS  ********/
input.side, input.blue  {
width:100px;
background-color:#77abb8;
font-weight: bold;
font-size:1em;
margin-top:2px;
color: #f0f0f0;
margin-left:20px;
height:23px;
}

input.blue {
width:40px;
}
/****************************    SEARCH BAR  ************/
#search {
position:absolute;
top:5px;
right:10px;
}



/******************************  SIDE NAVIGATION    ****/
#sidenav    {
float:left;
margin-left:20px;
margin-top:20px;
width:200px;
border-left:1px solid #c4c4c4;
border-right:1px solid #c4c4c4;
border-bottom:1px solid #c4c4c4;
}

#sidenav ul  {
list-style-type:circle;
}

#sidenav p.search  {
padding-left:1em;
padding-bottom:10px;
}

#sidenav  li  {
padding-top:0.6em !important;
padding-top:0.4em;
}

#sidenav li a {
color:#000;
}

#sidenav li a:hover  {
color:#2279af;
}


#sidenav h2 {
font-size:100%;
background:url(<?php root ?>/includes/teal.gif);
color:#fff;
height:1.8em;
line-height:1.8em;
padding-left:15px;
margin-top:0;
}

/*************************    MAIN CONTENT AREA  ******* */
#content  {
width:66%;
float:left;
margin-left:30px;
margin-top:20px;
}


#content  p {
text-indent:20px;
}

#content a {
color:#77aab8;
text-decoration:none;
}

#content a:hover {
text-decoration:underline;
}

#content img {
float:left;
}


#content h1 {
height:1.8em;
line-height:1.8em;
background:url(<?php root ?>/includes/grey.gif) repeat-x;
border-top:1px solid #c4c4c4;
margin-top:0;
font-size:1.2em;
color:#000;
padding-left:5px;
}

/*  *************************    FOOTER  *****************  */
#footer  {
clear:both;
height:3em;
border-top:1px solid #77abb8;
text-align:center;
color:#000;
margin-top:2em;
font-size:.8em;
}

#footer a {
color:#000;
}

#ads {
clear:both;
height:3em;
text-align:center;
}
[/code]

style2.php
[code]
/* CSS Document */

#sidenav, #topnav, #navwrap, #cornernav {
display:none
}

#content h1 {
background:#fff;
border:0;
}

#content {
width:100%;
}

#title {
text-align:center;
}

#footer {
border:0;
}

[/code]

sorry it took so long to test.  you posted your solutions and I haven't had time to do any coding on top of homework, work, and college activities.

Share this post


Link to post
Share on other sites
Have you tried placeing a

[code=php:0]
header("Content-type: text/css");
[/code]

at the top of your css files?

Here is an article that is somewhat related to your question. This is manly geared towards using php to avoid [url=http://www.stylegala.com/articles/no_more_css_hacks.htm]Css Hacks[/url]. If I were you I would read some of the comments.

Good Luck,
Tom

Share this post


Link to post
Share on other sites
background:url(<?php root ?>/includes/teal.gif) repeat-x;
Short hand isn't safe
use <?php echo $root; ?>
if that's what you are trying to do, and he's right you didn't try passing the header.
It looks like your using some form of templating system.  It iwll probably work once you pass it that header, it won't work if you don't, that tells the browser that it's a css file.  It also has to be valid css, that hte php outputs.

Share this post


Link to post
Share on other sites
You could also place this in a htaccess to tell apache to parse css like php.

[code]
AddHandler application/x-httpd-php .css

[/code]

This is what I do..

Tom

Share this post


Link to post
Share on other sites
it is saved as .php, it has the header thing in it, and i even did businessmans suggestion. nothing nothing, and nothing. :(

Share this post


Link to post
Share on other sites
The PHP engine never sees that CSS file. The client's browser finds the file when it sees the <link> tag, and it never goes through the PHP engine - the client requests that file from apache, which delivers it just as it sees it.\

tomfmason's suggestion will almost work, but he's got a couple Apache directives and their syntax mixed up. The directives you would want to set would be either:

[code]AddHandler x-httpd-php .css[/code]
or
[code]AddType application/x-httpd-php .css[/code]

And you'll want to make sure that you set the variable $path in the CSS file - but since I think you're going for portability here, that doesn't make sense at all. It would make more sense to just hard-code it into the CSS file, since that's effectively what would be going on anyway.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.