Jump to content

AutoChoosing a CSS file based on URL


johnathanamber

Recommended Posts

Hey everyone,

 

I have a site that has multiple URLs and CSS files.

 

Based on the URL that the user goes to I want that CSS file to load.

 

This is what I have thus far in the <head> of my HTML/PHP file: index.php

<!-- ====================================================== -->
<!-- Which CSS template to use? It depends on the site URL. -->
<!-- ====================================================== -->

<?php $site = $_SERVER['SERVER_NAME']; ?>

<!-- ====================================================== -->
<!-- ============== Harvest Auction Domains =============== -->
<!-- ====================================================== -->
<?php if ($site = "www.agcooponline.com") { ?>
	<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>
<?php if ($site = "www.agliberty.com") { ?>
	<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>
<?php if ($site = "www.harvestauction.com") { ?>
	<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>
<?php if ($site = "www.harvestauction.net") { ?>
	<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>
<?php if ($site = "www.harvestauction.org") { ?>
	<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>
<?php if ($site = "www.harvestauction.biz") { ?>
	<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>
<?php if ($site = "www.harvest2market.com") { ?>
	<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>

<!-- ====================================================== -->
<!-- ============== Hobbies Auction Domains =============== -->
<!-- ====================================================== -->

<?php if ($site = "www.hobbiesauction.com") { ?>
	<link href="../templates/tsgcalpha/css/hobbiesauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>

<!-- ====================================================== -->
<!-- ============== Tools Auction Domains ================= -->
<!-- ====================================================== -->

<?php if ($site = "www.toolsforauction.com") { ?>
	<link href="../templates/tsgcalpha/css/toolsforauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>
<?php if ($site = "www.tooleagle.com") { ?>
	<link href="../templates/tsgcalpha/css/toolsforauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>
<?php if ($site = "www.tooltwister.com") { ?>
	<link href="../templates/tsgcalpha/css/toolsforauction.css" rel="stylesheet" type="text/css" media="all" />
<?php } ?>

<!-- ====================================================== -->
<!-- ============== Aviation Auction Domains ============== -->
<!-- ====================================================== -->

<?php if ($site = "www.aviatorsauction.com") { ?>
	<link href="../templates/tsgcalpha/css/aviatorsauction.css" rel="stylesheet" type="text/css" media="all" />
<?php }  ?>

</head>

 

However when I go to any of the URLs it always sees www.aviatorsauction.com.

 

I have used both $_SERVER['SERVER_NAME']; and $_SERVER['HTTP_HOST'];

 

Any help would be greatly appreciated.

 

Thank you and God bless,

Johnathan

Link to comment
Share on other sites

Try this and see if it works,

 

p.s. switch is there for a reason

 

<?php
function get_css($baselink='../templates/tsgcalpha/css/',$hostname=NULL) {
	$hostname = $hostname === NULL ? $_SERVER['SERVER_NAME'] : $hostname;
	switch($hostname) {
		case "www.agcooponline.com":
		case "www.agliberty.com":
		case "www.harvestauction.com":
		case "www.harvestauction.net":
		case "www.harvestauction.org":
		case "www.harvestauction.biz":
		case "www.harvest2market.com":
			$css_file = "harvestauction.css";
			break;
		case "www.hobbiesauction.com":
			$css_file = "hobbiesauction.css";
			break;
		case "www.toolsforauction.com":
		case "www.tooleagle.com":
		case "www.tooltwister.com":
			$css_file = "toolsforauction.css";
			break;
		case "www.aviatorsauction.com":
			$css_file = "aviatorsauction.css";
			break;
	}
	return $baselink . $css_file;
}
?>
<head>
<link rel="stylesheet" href="<?php print get_css(); ?>" type="text/css" />
</head>

Link to comment
Share on other sites

@crabfinger,I will certainly give that a try later.

 

Curious though, the code that I posted, can that work?

 

Thank you and God bless,

Johnathan

 

I'm sure yours could work, I don't know because I would never write something that long to do something so easy.

 

oh and try this before the code

 

print $_SERVER['SERVER_NAME']

Link to comment
Share on other sites

That is some crazy code you got there.  It didn't work because your really not doing anything.  Your opening and closing your php tags and setting the $site var but thats it.  In order for it to work right you would need it to look like.

<?php
if ($site = "www.agcooponline.com") {
echo  '<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />';
}
elseif ($site = "www.agliberty.com") {
echo  '<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />';
}
// etc....
?>

 

If you had to code it this way you would use if elseif instead of all if's.  That way when it finds a match it stops looking and  doesn't have to spend time searching through the others.

 

But crabfinger is right,  this is the sort of thing switch was made for. 

Link to comment
Share on other sites

your just not echoing out. 

 

<?php if ($site = "www.agcooponline.com") { ?>

<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />

<?php } ?>

 

should be

 

<?php if ($site = "www.agcooponline.com") { ?>

echo "<link href='../templates/tsgcalpha/css/harvestauction.css' rel='stylesheet' type='text/css' media='all'>" />

<?php } ?>

 

 

But please don't do this, re write it, your code is very very messy.

Link to comment
Share on other sites

to the last two posters

 

you will notice that after the if statement the op uses a php closing tag so that if the data is selected properly it will display that html. I do it all the time when I don't want to put huge amounts of code into a variable. What the op was having a problem with was not that it was not displaying, in fact it was, it was just displaying the wrong one.

Link to comment
Share on other sites

  • 2 weeks later...

Hey everyone,

OK, what was posted works great! Thanks guys.

 

I actually used the same code to switch the Google Analystics code for each URL. That also works well.

 

<?php

function get_css($baselink='../templates/tsgcalpha/css/',$hostname=NULL) {

$hostname = $hostname === NULL ? $_SERVER['SERVER_NAME'] : $hostname;

switch($hostname) {

case "www.agcooponline.com":

case "www.agliberty.com":

case "www.harvestauction.com":

case "www.harvestauction.net":

case "www.harvestauction.org":

case "www.harvestauction.biz":

case "www.harvest2market.com":

$css_file = "harvestauction.css";

break;

 

case "www.hobbiesauction.com":

$css_file = "hobbiesauction.css";

$analytics = "hbaanalytics.html";

break;

 

case "www.toolsforauction.com":

case "www.tooleagle.com":

case "www.tooltwister.com":

$css_file = "toolsforauction.css";

break;

 

case "www.aviatorsauction.com":

$css_file = "aviatorsauction.css";

break;

}

return $baselink . $css_file;

}

?>

 

I have one more question... I need the $baselink to show the URL WITHOUT the 'www.'.

 

What is the best way to do this?

 

The reason is due that if you enter the URL without the 'www.' then the CSS files, and also the Google Analystics, won't load. This obviously won't work.

 

Thank you and God bless,

Johnathan

Link to comment
Share on other sites

OP: There was nothing MAJORLY wrong with your first code..

The only thing you did wrong was the use of '=' and not '==' in your if statements.

= is for assignment, whereas == is for comparision so essentially you were saying

if ($site = 'somesite') which in ALL cases is true because you are assigning 'somesite' to $site;

I bet if you had of viewed your output source code ALL of the stylesheets would have been included onto your page..

 

All the other comments saying that you aren't echoing anything and all your doing is opening and closing PHP tags is incorrect. Your code WOULD have worked, although it is long, drawn out and messy.

crabfingers solution is alot cleaner..

Link to comment
Share on other sites

@Buddski,

 

Thank you for the info... any ideas on my last question...

 

I have one more question... I need the $baselink to show the URL WITHOUT the 'www.'.

 

What is the best way to do this?

 

The reason is due that if you enter the URL without the 'www.' then the CSS files, and also the Google Analystics, won't load. This obviously won't work.

 

And you were correct about all of the code displaying on the site at the same time.

 

Thank you and God bless,

Johnathan

Link to comment
Share on other sites

$newurl = substr($baseurl, 4, strlen($baseurl)); 
echo $newurl; //  Example result: site.com/test.html 

 

That should extract anything after the www. Note it must contain " www. " or it will overwrite the url. You can check for the www using strpos if required.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.