Forum Moderators: coopster

Message Too Old, No Replies

How do I pass variables between three different files?

         

wavesurf

12:35 pm on Jul 17, 2006 (gmt 0)

10+ Year Member



I have a question that is a variation of this [webmasterworld.com] (also a topic started by me - so I really don't grasp this information at all :) )

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.

eelixduppy

12:38 pm on Jul 17, 2006 (gmt 0)



include variables.php in your stylesheet if it is already parsed as php:

[url=http://us2.php.net/include/]include[/url](variables.php);

Hope this helps

wavesurf

12:52 pm on Jul 17, 2006 (gmt 0)

10+ Year Member



Thanks. I do know that this will work.

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?

trillianjedi

12:55 pm on Jul 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're using php to render the CSS file, how about passing it the vars with arguments?

example.com/thefile.css?var1=fred&var2=bozo&var3=hello

eelixduppy

12:56 pm on Jul 17, 2006 (gmt 0)



Well....Instead of linking in the stylesheet, you can include it in the correct place. That way, when you include the variables.php file at the top of the page, they will work for everything. Or you can experiment with Sessions [us2.php.net] to get your desired outcome.

wavesurf

6:27 pm on Jul 17, 2006 (gmt 0)

10+ Year Member



Ok, what do you mean is the correct place?

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.

eelixduppy

6:33 pm on Jul 17, 2006 (gmt 0)



Sorry. I meant something like this:

<?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.

wavesurf

7:06 pm on Jul 17, 2006 (gmt 0)

10+ Year Member



Ahh. I understand. So instead of linking it in as usual, just include it in between the style-tags.

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.

coopster

9:04 pm on Jul 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




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.

wavesurf

4:39 am on Jul 18, 2006 (gmt 0)

10+ Year Member



Ok, great. That was the answer I was looking for :)

Thanks everyone for all your help.

leadegroot

6:18 am on Jul 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alternately, break it down smaller:
Move the variables that apply to the css file to a separate file eg cssvariables.php

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? :)

bgirl

6:02 am on Aug 5, 2006 (gmt 0)

10+ Year Member



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]