Jump to content

PHP to switch Stylesheets


hp1

Recommended Posts

Hi all, Been a while since I posted, the site has been neglected :) I have been using a fairly standard 3 column layout using a single external style sheet and a tiny bit of in-line style. However there a couple of pages on my site that I would just like to have two columns, i.e. one small one for the navigation and a large one for the content.Because I call all the seperate contant files in the main index.php which has a standard <head> i need to know how to tell the <head> which .css file to use when a link is clicked.The code I have in index.php for the div in question is:

<div id="centercol">	<?php	if (isset($_GET['page'])){	$page=$_GET['page']; 	include "$page.php";	  	}else{	include ('indexhome.php');	}	?>			</div>

So how do i call a stylesheet in the <head> for a specific $page.php? Does that make sense?Cheers

Link to comment
Share on other sites

Hi all, Been a while since I posted, the site has been neglected :) I have been using a fairly standard 3 column layout using a single external style sheet and a tiny bit of in-line style. However there a couple of pages on my site that I would just like to have two columns, i.e. one small one for the navigation and a large one for the content.Because I call all the seperate contant files in the main index.php which has a standard <head> i need to know how to tell the <head> which .css file to use when a link is clicked.The code I have in index.php for the div in question is:
<div id="centercol">	<?php	if (isset($_GET['page'])){	$page=$_GET['page']; 	include "$page.php";	  	}else{	include ('indexhome.php');	}	?>			</div>

So how do i call a stylesheet in the <head> for a specific $page.php? Does that make sense?Cheers

the same thing you are doing with the include '$page.php' where your getting the variable right?get a variable for what css file you want to use and do<head><link rel="stylesheet" type="text/css" href="<?php echo $var ?>.css"></head>
Link to comment
Share on other sites

Also, your code is not secure. Anyone could try guessing pages on your server to get to see them.Make sure to use an array on what pages are allowed and check to see that is what the user is getting access to.Change this$page=$_GET['page'] to $page=basename($_GET['page'])

Link to comment
Share on other sites

the same thing you are doing with the include '$page.php' where your getting the variable right?get a variable for what css file you want to use and do<head><link rel="stylesheet" type="text/css" href="<?php echo $var ?>.css"></head>
Ok, i figured it would be something like that, I'm just dont know enough php to work it all out. So where do I place the code where I get the variable? I'm assuming i cant us ethe same code used below in the Divs because its in the wrong place? Does it go in the <head> as well but before the <link rel>, sorry for silly questions. MMA thanks for the warning about the code, i had no idea!cheers
Link to comment
Share on other sites

The position of the code doesn't matter as long as it is before whatever you want to do.
Ok, i'm just a little confused:I need to do something like:If this link is clicked then use this style sheet, else use this one.
Link to comment
Share on other sites

Right. So you need to set a variable that says which style sheet to use, maybe the variable contains the name of the stylesheet like astralaaron showed. If you want it on a "per-link" basis, then you would need to add a variable in the URL for links that this would apply to. Then you would check if that variable exists, and if it does, use the special stylesheet or else use the default stylesheet. Or, if you want it on a "per-page" basis, where a certain page would use a certain stylesheet regardless of which link they clicked on to get there, then you would want to have a list of pages that use the special stylesheet and check on each page if you are on one of the special pages, and use the appropriate sheet.The point is that somehow you need to figure out which stylesheet to use, either based off a variable in the URL or the name of the page itself, store the name of the stylesheet to use in a variable, and then use the code that astralaaron wrote to print the stylesheet name inside the link tag.

Link to comment
Share on other sites

JSG. Thanks for the detailed response. Thats really helpful. However, I'm still getting to grips with how to combine HTML and PHP. The piece of code I posted above I understand: Retrieve a link and store it in a variable, then if the variableb is this load this code if not, load this code. If i undertand correctly i can do something similar as you have explained. What I'm still failing to understand is the actual syntax and and layout. In the code above all the php was contained within one set of <?php code here ?>. Whereas now it look like I'll need several blocks as astralaaron has seperate php tags in the LInk rel. something like:

<?php	if (isset($_GET['page'])){	$var=$_GET['page']; ?><link rel="stylesheet" type="text/css" href="<?php echo $var ?>.css">  	   <?php   }else{<link rel="stylesheet" type="text/css" href="threecolumns.css">	}	?>

I know this code is horribly wrong but its the bits where I need to include the <Link Rel> ui'm struggling with. Sorry to keep bugging ya for help (I just ordered a php book off amazon so hopefully these daft questions shoudl reduce!)Cheers

Link to comment
Share on other sites

The idea is to first perform all negotiations in PHP and then use the ONE result of it ONCE. In order to use a result that was created in PHP, you need to initiate PHP again at the desired spot (in this case, the "href" attribute). So instead of having two <link/> elements, you'll have one, but the value of it's "href" attribute will change depending on the desired stylesheet. So:

<?php	if (isset($_GET['page'])){	$var=$_GET['page'];}else {$var = 'default';} ?><link rel="stylesheet" type="text/css" href="<?php echo $var ?>.css">

reads 'if the URL to the page has a variable "page", use this value as the location of the stylesheet. Otherwise, use "default.css" as the stylesheet.'For example, if the URL is:

index.php?page=mypage

then the stylesheet will be

mypage.css

while

index.php?page=myOTHERpage

will use the stylesheet

myOTHERpage.css

and the url

index.php

will use

default.css

since no "page" variable was provided.

Link to comment
Share on other sites

Boen, Thats excellent, just what I was looking for. I've no problem with the CSS or the HTML, more how PHP and HTML work together and how syntax looks. Your detailed and simple response is what I have come to appreciate in this excellent forum.Cheers

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...