Change Your Website’s Appearance With The Weather

February 9, 2010 · Filed Under Tips & Tricks, Web Development · 1 Comment 

This post is based on this tutorial. However, I made a few modifications to the way it’s displayed.

The PHP code is the same and can be seen below:

<?php
 
	/* get the weather from Yahoo */
	$data = get_data("http://weather.yahooapis.com/forecastrss?p=XXXXX&u=f");
 
	$weather_class = format_result(get_match('/<yweather:condition  text="(.*)"/isU',$data));
 
	/* format the result */
	function format_result($input)
	{
		return strtolower(str_replace(array(' ', '(', ')'), array('-', '-', ''), $input));
	}
 
	/* helper:  does regex */
	function get_match($regex,$content)
	{
		preg_match($regex,$content,$matches);
		return $matches[1];
	}
 
	/* gets the xml data from Alexa */
	function get_data($url)
	{
		$ch = curl_init();
		$timeout = 5;
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
		$xml = curl_exec($ch);
		curl_close($ch);
		return $xml;
	}
 
?>

The only thing you have to change within the PHP is the line below:

$data = get_data("http://weather.yahooapis.com/forecastrss?p=XXXXX&u=f");

Replace the XXXXX with your zip code.

The PHP code goes out and grabs the weather condition from Yahoo. Then you can define a class with it like this:

<div class="header <?php echo $weather_class; ?>">
</div>

After that, we need to style it with CSS and tell it what images to use as the background. With my CSS, I actually include all the conditions, instead of just a few.

.cold,.dust,.foggy,.haze,.smoky,.blustery,.windy,.sunny,.fair-day,.hot{background: url('../images/weather/clear.png') no-repeat top right}
.clear-night,.fair-night{background: url('../images/weather/clear-night.png') no-repeat top right}
.partly-cloudy-day,.partly-cloudy{background: url('../images/weather/few-clouds.png') no-repeat top right}
.partly-cloudy-night{background: url('../images/weather/few-clouds-night.png') no-repeat top right}
.cloudy,.mostly-cloudy-day,.mostly-cloudy-night{background: url('../images/weather/overcast.png') no-repeat top right}
.tornado,.tropical-storm,.hurricane{background: url('../images/weather/severe-alert.png') no-repeat top right}
.hail,.sleet,.thundershowers,.mixed-rain-and-hail{background: url('../images/weather/showers.png') no-repeat top right}
.mixed-rain-and-snow,.mixed-rain-and-sleet,.freezing-drizzle,.drizzle,.freezing-rain,.showers,.scattered-showers{background: url('../images/weather/showers-scattered.png') no-repeat top right}
.mixed-snow-and-sleet,.snow-flurries,.light-snow,.light-snow-showers,.blowing-snow,.snow,.heavy-snow,.scattered-snow-showers,.snow-showers{background: url('../images/weather/snow.png') no-repeat top right}
.severe-thunderstorms,.thunderstorms,.isolated-thunderstorms,.scattered-thunderstorms,.isolated-thundershowers{background: url('../images/weather/storm.png') no-repeat top right}

I’ve also packaged up all the icons that I used. There are three different sizes that you can choose. You can download them from here. They are originally from the Tango icon set.

If you want to see it in action, you visit this site and look in the upper-right corner. If you have any questions, let me know.

Easily Redirect To A Random URL Using PHP

June 29, 2009 · Filed Under Scripts · Comment 

Using the code below, you can send a visitor to one URL and then have them redirected to a random URL. All you have to do is open notepad, copy the code below, change the addresses and then save it as “whatever.php”. You can also add more addresses by adding more lines and changing the number.

<?php
$url[0] = "http://www.google.com";
$url[1] = "http://www.yahoo.com";
$url[2] = "http://www.47movies.com";
 
srand ((double)microtime()*1000000);
$randomnum = rand(0, count($url)-1);
 
header ("Location: $url[$randomnum]");
?>

Right now, I’m using this to redirect visitors to a random sponsor on a website that I recently closed down. I redirect all 404s to the index of the site using htaccess and then the index.php file contains the random redirect.

Random Text Link Script

December 18, 2008 · Filed Under Scripts · Comment 

This is a very simple script that will insert a random text link in a desired location. All you have to do is copy the text below and paste it in notepad and name it “random.php”.

<?
$random_url = array("http://www.google.com/1",
                    "http://www.google.com/2",
                    "http://www.google.com/3",
                    "http://www.google.com/4",
                    "http://www.google.com/5");
 
$url_title = array("Google Link 1",
                   "Google Link 2",
                   "Google Link 3",
                   "Google Link 4",
                   "Google Link 5");
srand(time());
$sizeof = count($random_url);
$random = (rand()%$sizeof);
print("<a href="$random_url[$random]">$url_title[$random]</a>");
?>

Then, insert the following code in your php file where you want the link to appear.

<?php include ("random.php"); ?>

Enjoy!