Jump to content


Photo

including php constants in css file - help!


  • Please log in to reply
7 replies to this topic

#1 Kingskin

Kingskin
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 July 2006 - 05:27 PM

OK good people of php freaks, here's one for you:

I have an external stylesheet that I want to include php a php constant in (the constant is a url, idea being that making themes is easy - i just set the constant to the location of my theme and bingo).

To do this I'm saving my css as a .php file with the following code at the top:

header('Content-type: text/css');

I then include my constants file, and then the stylesheet like this:

include_once("includes/define.php");
...
<link href="css/main.php" rel="stylesheet" type="text/css" />

that works fine, my css is displayed as it should be, except that my defined constant is not available within the main.php css file. I can echo it out fine above or below the style sheet link and i get the value, but within that script itself it's empty. I can define the same constant within the css script and it all works fine, but the idea was the contant could be set via a database, so there would be no need to edit any source code to change themes.

I then thought if I just run the query to define the constant inside the css script, that would work fine. I tried that, but it won't work - no error message, just an empty result.

So, I'm at a loss. Anyone know why this is happening and how I can get round it?

Cheers.

#2 kalivos

kalivos
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationCalifornia

Posted 21 July 2006 - 05:34 PM

That's an interesting way to do it. I'm sure I'm doing it incorrectly, but I just include_once("css.php") rather than trying to use <link ....> My way of doing it displays the CSS on the current page rather than "including" it as a link referance. Thus, it's a little sloppy. But it works :)
Make a donation to phpfreaks. They need your help![br][br]

#3 Kingskin

Kingskin
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 July 2006 - 06:44 PM

That's an interesting way to do it. I'm sure I'm doing it incorrectly, but I just include_once("css.php") rather than trying to use <link ....> My way of doing it displays the CSS on the current page rather than "including" it as a link referance. Thus, it's a little sloppy. But it works :)


and are you using php constants or variables inside your css? I'd really like to keep the css separate if I can though.

#4 kalivos

kalivos
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationCalifornia

Posted 21 July 2006 - 06:58 PM

I use both, depending on which site I refer too  ;D I'm currently making a site with user switchable themes and styles. I'm trying out a new approach of using $_SESSION(). This is my way of trying out different formats.

<link href="<? echo $_SESSION['theme']; ?>/css/main.css" rel="stylesheet" type="text/css" />

This way the CSS is constant and user selects the theme. Experiment and have fun  :D
Make a donation to phpfreaks. They need your help![br][br]

#5 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 21 July 2006 - 07:21 PM

well the thing is, your HTML would access the CSS via HTTP as far as i know and PHP would have parsed the main.php file via Apache by then.

What you could do is pass the variables to CSS via the URL:

main.php
<?php
	header('Content-type: text/css');
?>
body	{background: #<?php echo $_GET['bg']; ?>;}


HTML file
<link href="main.php?bg=0f0" rel="stylesheet" type="text/css" />


#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 21 July 2006 - 10:17 PM

by using LINK, main.php is treated as a separated script and would not see the variable scope of the current page as Koobi mentioned. you could use Koobi's methed, or include define.php within main.php
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 Kingskin

Kingskin
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 July 2006 - 10:40 PM

I use both, depending on which site I refer too  ;D I'm currently making a site with user switchable themes and styles. I'm trying out a new approach of using $_SESSION(). This is my way of trying out different formats.

<link href="<? echo $_SESSION['theme']; ?>/css/main.css" rel="stylesheet" type="text/css" />

This way the CSS is constant and user selects the theme. Experiment and have fun  :D


OK, that's a slightly different interpretation of the thing i'm trying - you have a css file for each theme which is something I need to incorperate. A basic one for the regular, theme-independant one and then the one for the theme. The way you are doing things was what I was going to do if I couldn't get it working this way, but once I try something and it doesn't work I like to know why!

well the thing is, your HTML would access the CSS via HTTP as far as i know and PHP would have parsed the main.php file via Apache by then.

What you could do is pass the variables to CSS via the URL:

main.php

<?php
	header('Content-type: text/css');
?>
body	{background: #<?php echo $_GET['bg']; ?>;}


HTML file
<link href="main.php?bg=0f0" rel="stylesheet" type="text/css" />


by using LINK, main.php is treated as a separated script and would not see the variable scope of the current page as Koobi mentioned. you could use Koobi's methed, or include define.php within main.php


Yup, perfect thanks. I passed the constant to main.php via the url and it worked perfectly. Thanks for the help.

#8 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 22 July 2006 - 04:02 AM

by using LINK, main.php is treated as a separated script and would not see the variable scope of the current page as Koobi mentioned. you could use Koobi's methed, or include define.php within main.php

i don't know why i didn't think to include define.php in main.php
i think that would be a better solution than passing the values via the URL.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users