Forum Moderators: coopster
I have a main.php file which contains the main document. At the beginning I include variables.php, a file that contains all my variables. Then in the header I link in the stylesheet style.css (have set the server to parse css-documents for php, which it does successfully).
The question is: How do I get a variable defined in the variables.php file to work inside the css-document?
Any takers?
Thanks for any help.
[url=http://us2.php.net/include/]include[/url](variables.php);
Hope this helps
However, I don't really want to include the variables.php-file twice, since I need the other variables for the main.php-script. I also don't want to make a css-specific variablesCSS.php-file. So I'm still looking for a way to pass variables between three files.
Is it just not possible, or have I not found the right solution yet?
I do link to the variables.php at the top of the page (main.php), but it doesn't seem to work for everything, because when I try to call the variable $example (which was declared in variables.php) in the stylesheet (which is parsed by the server) it gives me an NOTICE: Undefined variable: example in style.css on line ....
I could, as you point out, probably solve the problem by putting the variables in Sessions, but I was still hoping there was a way I could call the variables within the stylesheet.
Can someone give me a definitive answer on whether this is impossible within PHP, or not?
Thanks again.
<?php include("variables.php");?>
<html>
<head>
<style>
<? include("stylesheet.css");?>
</style>
</head>
<body>
Other stuff
<? include("phppage.php");?>
</body>
</html>
Umm...yea...that is what I meant. Something like that should work.
That does work, thanks.
However, I'm still waiting to hear a definitive answer whether it is possible to call a variable passed between three files, like I outlined above.
Also, although this probably is a CSS-question: Is there any real difference between linking in a stylesheet using the link-tag and including a css-file within style-tags in the head-tag of the document.
Thanks again for any help.
However, I'm still waiting to hear a definitive answer whether it is possible to call a variable passed between three files, like I outlined above.
Yes it is possible and the method described by eelixduppy is how to do so. You can also pass your variables in the QUERY_STRING as described by trillianjedi. However, if you are trying to parse variables in style.css where the file is being retrieved via @import or the <link> element, then no, the variable is not going to be defined in that scope -- it is a completely different GET request outside of the scope of the main page's GET request.
So
you have:
cssvariables.php
// the variables that only apply to the css
variables.php
include_once 'cssvariables.php';
// add the rest of the variable shere
main.php
include_once variables.php
// so you get the css variables and the general ones, because the include will 'chain'
file.css
include_once 'cssvariables.php';
// css variables here only
Does that make sense? :)
Also, although this probably is a CSS-question: Is there any real difference between linking in a stylesheet using the link-tag and including a css-file within style-tags in the head-tag of the document.
Only difference is the cascading... whatever style is defined last is the style that is applied. Styles that are defined in the document itself, between the <style> tags take precedence.
5. <link href="style_global.css" rel="stylesheet" type="text/css" />
4. <link href="style_mypage.css" rel="stylesheet" type="text/css" />
3. <link href="style_box.css" rel="stylesheet" type="text/css" />
2. [b]<link href="style_global_v2.css" rel="stylesheet" type="text/css" />[/b]<style type="text/css">
<!--
1. [b]#myStyle {margin:50px;}[/b] // This is the style that shows up
-->
</style>
Sometimes I use that to change a background on a button with php in the document.
.genericButton a {background:(basicbutton.gif); margin:10px; padding:5px;} in the document I redefine only the background image:
<style>
<!--
.genericButton a {background:(<?php print $mybutton;?>;}
-->
</style> When you validate your css you may get a warning that a style was redefined, but it still validates without errors.
[edited by: coopster at 4:59 pm (utc) on Aug. 5, 2006]
[edit reason]
[1][edit reason] Disable graphic smile faces [/edit] [/edit][/1]