Topic: passing variables to external css? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=28816" title="Pages that link to Topic: passing variables to external css? (Page 1 of 1)" rel="nofollow" >Topic: passing variables to external css? <span class="small">(Page 1 of 1)</span>\

 
CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 01-11-2007 22:19

Here is what I am trying to do. I have pages that are loaded from a php include. I need to pass variables to an external style sheet as well. It can be a lot of variables depending on the page.

I have right now in the index.php page:

code:
switch ($_GET['link']){

	case 'page1':
		$page = "pages/page1.php";
		$bodyClass="page1";
		$bgColor = "#008000";
		break;

        case 'page2':
                 $page = "pages/page2.php";
                 $bodyClass="page2";
		 $bgColor = "#800000";
		 break;
                  
	default :
		$page = "pages/main.php";
		$bodyClass = "main";
		$bgColor= "#8080FF";
}



Then in the external css page I have this (which is actually a php page with a header defined for css:

code:
<? header("Content-type: text/css");  ?>

body {
 background:<?=$_GET['bgColor'];?>;
 
}



but obviously this isn't working. I was trying to call the pages and the changes by this :

code:
<div>
	<a href="index.php?link=page1">Page 1</a><br />
	<a href="index.php?link=page2">Page 1</a><br />
	<a href="index.php?link=main">Main</a>
</div>



I also tried to pass the background variables through the URL but that didn't seem to work either. Like :

code:
<a href="index.php?link=page1&bgColor=#800000">Page 1</a><br />



not only does that look ugly, it didn't work. So does anyone have a good suggestion on how to pass these variables from the external css to the index page?

Thanks in advance!


oh, and the $bodyClass sets the id of the body depending on what page they are on. This works but that would mean that I would have to duplicate the same style for all the different pages and just make some changes.

Later,

C:\

(Edited by CPrompt on 01-11-2007 22:22)

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 01-11-2007 22:37

make sure to pass the parameters in GET to the external stylesheet, i.e:

code:
<!--here be doctype declaration, head, ... -->

<link type='stylesheet' media='screen,printer' href='style.php?link=<? echo $_GET['link']; ?>' />

<!-- here be monsters, script tags, the body and all the malarkey -->

Hope that helps

Another idea would be to use a session variable that you set in the main PHP script and use it in the 'child' script such as the one generating the stylesheet.



(Edited by poi on 01-11-2007 22:38)

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 01-11-2007 23:45

I think you may be overcomplicating the issue. Is the <body> the only element to be affected?

If so, I would do soemthing like this:

<body class=" <?php echo $bodyClass; ?> ">

If it is more complicated, I would use the defined page to call a secondary stylesheet, after the first, which would detail the page specific styles. This second stylesheet would overide any styles set by the primary CSS file.

Of course, if it is only slightly more complciated, you can use simple cascading in your stylesheet -

code:
body.page1 h1 {

...styles...
}



etc.

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 01-11-2007 23:52

DL-44: sure, that's the cascading is one of the biggest strength of CSS. Add to that that having a single biiiig stylesheet that can be cached beats having a zillion of smaller not so cachable ( due to the arguments passed in GET ) stylesheets any time.

CPrompt: Sorry I skimmed through you post and missed some obvious details. One more thing, you should really consider doing some URL rewrite and get rid of the ugly index.php?link=XYZ&stuff If you're not familiar with MOD_REWRITE, one easy way to do that is to simply catch the 404 and treat the requested URI to return the requested content or a 404



(Edited by poi on 01-12-2007 00:01)

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 01-12-2007 00:42

DL-44: The body class is just one that would be affected. There are a bunch more depending on the page. The "About Us" page might have 5 - 6 elements that would be different than the rest, the "Contact Us" page would have maybe 10 (just guessing on the numbers). That is why I just wanted to pass variables to the style sheet.

However, I think I might use your second suggestion on the separate style sheets. That might be the best thing to do until I can get the other going.

If I put in the main style sheet the stuff that will not change and then in the other style sheets just put the stuff that goes along with that page.

Question though (I can figure this out I guess) If in the main style sheet I put something like :

code:
body{
     margin:0px;
     padding:0px;
}



and then in the separate page put :

code:
body.page1{
     background-color:#8080FF;

}



The overall margin and padding of the body will still be 0 correct?

Thanks for the help!

Later,

C:\

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 01-12-2007 00:51

yep the margin and padding would still be 0

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 01-12-2007 02:03

Anything specified in the secondary stylesheet will overide the same specs in the primary.

Anything in the primary stylesheet, not specified in the secondary, will remain as specified.

I would recommend forgetting about passing variables *to* a stylesheet. I don't see how that would improve upon anything, and it would cause more work and potential problem - apart from being somewhat contrived.

Specifying multiple style sheets, or using one style sheet and multiple body classes are exactly the way this sort of thing was designed to work.

FWIW

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 01-12-2007 04:08
quote:

DL-44 said:

Specifying multiple style sheets, or using one style sheet and multiple body classes are exactly the way this sort of thing was designed to work.




Thanks. That did it just fine. Appreciate the help

Later,

C:\

skyetyger
Bipolar (III) Inmate

From: midair
Insane since: Jul 2001

posted posted 01-14-2007 02:02
quote:

poi said:

get rid of the ugly index.php?link=XYZ&stuff



I have that ugly code ..header <..then some code to redirect..
There is a line War Mage said to use..but I am not certain if that bit of code is "in place of" the Header<redirect code...
or if I have to rewrite the Server part as the name of my server..
Here is a portion of the War Mage code
$_SERVER['DOCUMENT_ROOT
Do I modify that? Is it to replace that header<php code redirect..
So new to this and it works on the site but..there must be a more "elegant" way to display on the address bar..
Any help with this appreciated...

(Edited by skyetyger on 01-14-2007 02:03)

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 01-14-2007 02:47

what you need to look at is mod_rewrite

I have very little knowledge of this personally, but google search on that term will bring you a ton of results.
You can also try 'search engine friendly url'

Basically what you do is tell the server to take a path such as /index/link/xyz and tell it to go to index.php?link=xyz
Makes for better search engine indexing, and a more informative user experience.

skyetyger
Bipolar (III) Inmate

From: midair
Insane since: Jul 2001

posted posted 01-17-2007 15:25

Thanks DL-44
I am so real life busy ..this is just a hobby so I am slow to reply..
I will do some research mod_rewrite
Just needed a pointer of where and what to look for..

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 01-18-2007 02:01
quote:

skyetyger said:

Just needed a pointer of where and what to look for..



http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

have fun with that I "might" be able to help you with that. I can post one that I use.

Later,

C:\

skyetyger
Bipolar (III) Inmate

From: midair
Insane since: Jul 2001

posted posted 01-18-2007 14:32

Thanks CPrompt
Post it if you would..Please and Thank you..
I am reading the mod_rewrite page..right now..
Thanks Again!

ted90
Nervous Wreck (II) Inmate

From:
Insane since: Apr 2016

posted posted 04-10-2016 20:31

long sought this information, thank you guys!
edit tp: spam removed

(Edited by Tyberius Prime on 04-10-2016 21:12)

Tao
Maniac (V) Inmate

From: The Pool Of Life
Insane since: Nov 2003

posted posted 04-11-2016 12:07

Go to bed Ted.

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 04-22-2016 07:29

Now that's some prime necroposting there, that is.


___________________________
Suho: www.liminality.org | Cell 270 | Sig Rotator | the Fellowship of Sup



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu