Jump to content
xisto Community
Sign in to follow this  
tjmcsax

Is This The Best Way To Do This? Linking PHP and CSS in Time

Recommended Posts

so my code is

<html><?	setlocale(LC_TIME, "C");	$tod = strftime ("%H%M");	if ($tod >  500 && $tod <= 1159)	{?> 	<style>	Body	{	background: yellow;	color: blue;	font-weight: bold;	}	a	{	color:lightgreen;	}	a:active	{	color:white;	}	ul	{	color:lightgreen;	}	div#banner	{	background-color: lightblue;	border-bottom: 1 px solid white;		}	div#banner h1	{	margin:0;	padding: .3em 0 .3em .5em;	font-size: 2.2em;	}	div#navigation	{	float:left;	width:200px;	padding-left: 1em;	padding-top: 2em;	padding-bottom: 1em;	background-color: green;	}	div#content	{	color: red;	margin-left: 225;	margin-top: 15;	font-size: 1em;	}	div#content h1	{	font-size: 2em;	}		</style><?	}	if ($tod > 1159 && $tod <= 1900)	{?>	<style>	Body	{	background: blue;	color: #00FFFF;	font-weight: bold;	}	a, ul	{	color:#FFFF3D;	}	a:active	{	color:blue;	}	div#banner	{	background-color: 000033;	border-bottom: 1 px solid white;		}	div#banner h1	{	margin:0;	padding: .3em 0 .3em .5em;	font-size: 2.2em;	}	div#navigation	{	float:left;	width:200px;	padding-left: 1em;	padding-top: 2em;	padding-bottom: 1em;	background-color: #0080FF;	}	div#content	{	color: white;	margin-left: 225;	margin-top: 15;	font-size: 1em;	}	div#content h1	{	font-size: 2em;	}		</style><?	}	if ($tod > 1900 || $tod <= 500)	{?>	<style>	Body	{	background-color: 0000000;	font-weight: bold;	}	a	{	color:FF0000;	}	a:active	{	color:white;	}	ul	{	color:FF0000;	}	div#banner	{	color: FF9900;	background-color: FF3300;	border-bottom: 1 px solid white;		}	div#banner h1	{	margin:0;	padding: .3em 0 .3em .5em;	font-size: 2.2em;	}	div#navigation	{	float:left;	width:200px;	padding-left: 1em;	padding-top: 2em;	padding-bottom: 1em;	background-color: yellow;	}	div#content	{	color: red;	margin-left: 225;	margin-top: 15;	font-size: 1em;	}	div#content h1	{	font-size: 2em;	}		</style><?	}?><body><div id = "banner"><h1>Timed Site Colors</h1></div><div id = "navigation">Navagation Bar<br><br><ul>	<li><a href="tod.txt">Site Source Code</a></li>	<li><a href="~klee1/PHP">PHP</a></li></ul></div><div id = "content"><h1>About This Site</h1><br>This site uses PHP to determine the time of day, and when it reaches a certain point of day it will change color scheme.  The code for this site found off of the navigation bar.<br></div></body></html>

I was wondering if there was a way that I could keep the size of the php script down so that it doesn't take up as much space as it does.

Is there a cleaner way to write this?

Share this post


Link to post
Share on other sites

If I'm reading this correctly, you plan to have different page themes based on the time of day?

If so, I would create a different CSS for each theme. Then using IF statements determine which theme to call and output the correct CSS link in the <head> tag.

<?phpsetlocale(LC_TIME, "C");$tod = strftime ("%H%M");if ($tod >  500 && $tod <= 1159) {$theme = "theme1.css";} if ($tod > 1159 && $tod <= 1900) {$theme = "theme2.css";} if ($tod > 1900 || $tod <= 500) {$theme = "theme3.css";}?><html>     <head>          <title>...</title>          <link href="http://mysite.astahost.com/<?php echo $theme; ?>" rel="stylesheet" type="text/css"/>     </head>     <body>...

Hope this helps, :)

vujsa

Share this post


Link to post
Share on other sites

vujsa's method looks good, although I haven't gone over it.When coding in php, avoid short tags, it's bad programming and a sloppy suggestion done by the PHP community in my opinion. It's only shaving a few characters but in my case, if you create your server so that it doesn't use short tags, you'll have fun fixing up what I do consider sloppy code, just because they give you short tags doesn't mean everyone runs their server this way. (same with using register_globals!)I definitely don't as I try to avoid cutting corners.If you want to know about sloppiness, then XAMPP is a prime example, I mean, they cut corners to give you an All-In-One package, (I still highly recommend it) but if you configure your server how you want, then the pages that they created, that use short tags and registered global variables just doesn't make it compatible at all.So just remember do <?php ?> instead of <? ?> and do <?php echo $variable; ?> instead of <?=$variable; ?>, one reason would be if you wanted to do the processing line in an XHTML php document <?xml version="1.0" encoding="utf-8"?>, it can have undefined behaviour.Cheers,MC

Share this post


Link to post
Share on other sites

tjmcsax,Can you just say what are the changes out of these CSS files, you could just use one CSS file and depending on the time condition, it could dynamically change the CSS for you, or it can load sections, or it can do pretty much what you want, just using 1 css file and going by the condition.I just noticed Vujsa uses the K&R source format for PHP, although it's really personal preference, I think the ANSI C source format is a better way of well-forming a PHP document, GNU source format may have some better logic to it, but personally ANSI C is nice and clean although it does push the code down further, but it makes it easier when tracing groups of statements inside the brackets. Don't worry I use to do K&R style and it took me a while to come round to ANSI C but you should look into this style.These different formatting ways are just guidelines to keeping code clean, it's not a must, although I do believe they are setting the standards for style, and surely a lot of developers do realise the importance for it, since it was them that helped make it.Problems with vujsa's way is there's no checking whether those files actually exist, nor do you have a default to fall back on if none of the conditions work, maybe a fault in the calculation, or a rip in the time nebula, or the planets have all aligned, but either way, checking and double checking is good practise. Don't really want to start fairly new people to PHP with bad practises. (I must remember to fix my code on here some day, hippocrit)Cheers,MC

Share this post


Link to post
Share on other sites

I got this code a while a go which compresses the CSS script before you send it which means even faster processing. Normally, this wouldn't be advised but since you are using PHP in the code anyway, it might be worth it.
Note: I am not sure if Xisto's PHP version supports this to test it and if it works let me know!

<?php ob_start ("ob_gzhandler");header("Content-type: text/css; charset: UTF-8");header("Cache-Control: must-revalidate");$offset = 60 * 60;$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s",time() + $offset) . " GMT";header($ExpStr);?>


My advice would be as mastercomputers suggested. Make one CSS file with the common attributes. Then change the colours in the XHTML file. It will save loads of time and space.

style.css
Body{font-weight: bold;}div#banner{border-bottom: 1 px solid white;}div#banner h1{margin:0;padding: .3em 0 .3em .5em;font-size: 2.2em;}div#navigation{float:left;width:200px;padding-left: 1em;padding-top: 2em;padding-bottom: 1em;}div#content{margin-left: 225;margin-top: 15;font-size: 1em;}div#content h1{font-size: 2em;}

Then the PHP at the top of the HTML would read;

<html><head><title>Changing With The Days</title><link rel="stylesheet" type="text/css" href="style.css" /><!--Begin PHP Style determination--><?setlocale(LC_TIME, "C");$tod = strftime ("%H%M");if ($tod >  500 && $tod <= 1159){?><style>Body {background: yellow;color: blue;}a, ul {color:lightgreen;}a:active {color:white;}div#banner{background-color: lightblue;}div#navigation {background-color: green;}div#content{color: red;}</style><?}elseif ($tod > 1159 && $tod <= 1900){?><style>Body {background: blue;color: #00FFFF;}a, ul {color:#FFFF3D;}a:active {color:blue;}div#banner {background-color: 000033;}div#navigation {background-color: #0080FF;}div#content {color: white;}</style><?}elseif ($tod > 1900 || $tod <= 500){?><style>Body {background-color: 0000000;}a {color:FF0000;}a:active {color:white;}ul { color:FF0000; }div#banner {color: FF9900;background-color: FF3300;}div#navigation {background-color: yellow;}div#content {color: red;}</style><!--End PHP--></head>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.