Categories

Get weather example

<?php
$city="Dundee";
$country="UK"; //Two digit country code
$url="http://api.openweathermap.org/data/2.5/weather?q=".$city.",".$country."&units=metric&cnt=7&lang=en";
$json=file_get_contents($url);
$data=json_decode($json,true);
//Get current Temperature in Celsius
echo "Temperature : " . $data['main']['temp']." c<br>";
//Get min current Temperature in Celsius
echo "Minimum Temperature : " . $data['main']['temp_min']." c<br>";
//Get max current Temperature in Celsius
echo "Maximum Temperature : " . $data['main']['temp_max']." c<br>";
//Get air pressure
echo "Air pressure : " . $data['main']['pressure']."<br>";
//Get humidity
echo "Humidity : " . $data['main']['humidity']."<br>";
//Get weather conditions
echo "Conditions : " . $data['weather'][0]['main']."<br>";
//Get cloud percentage
echo "Cloud percentage : " . $data['clouds']['all']." %<br>";
//Get wind speed
echo "Wind speed : " . $data['wind']['speed']." mph<br>";
//Get wind direction
echo "Wind direction : " . $data['wind']['deg']."<br>";
?>

Valid email address filter

<?php

$emailAddress = "my_email_test@mail server.com";

if(!filter_var($emailAddress, FILTER_VALIDATE_EMAIL))
{
    echo "Submitted address is faulty";
}
else
{
    echo "Address submitted successfully";
}

?>

Dynamic Title Tags

<title>
<?php
if (is_home()) 
{ 
echo bloginfo('name');
} 
elseif (is_404())
 {
echo '404 Not Found';
} 
elseif (is_category()) 
{
echo 'Category:'; wp_title('');
} 
elseif (is_search()) 
{
echo 'Search Results';
} 
elseif ( is_day() || is_month() || is_year() ) 
{
echo 'Archives:'; wp_title('');
} 
else 
{
echo wp_title('');
}
?>
</title>

Display Recent Posts

<?php query_posts('showposts=5'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>

PHP MySQL Data Connection Class

<?php
Class DataConnection {
	
	var $oConn;
	var $oDB;
	var $oCurrentResource;
	var $aResultSet;
	var $bResultsFound;


	function DataConnection()
	{
		$this->oConn = mysql_connect(DB_SERVER, DB_USER, DB_USER_PASS) or die("No connection!");
		$this->oDB = mysql_select_db(DB_NAME) or die("No database!");
		register_shutdown_function(array(&$this, "closeConnection"));
		
		$this->bResultsFound = FALSE;
	}

	function query($sSQL, $bDebugMode = FALSE)
	{
		if ($bDebugMode) {
			echo $sSQL."<br />";
			exit;
		}
		$this->setCurrentResource(mysql_query($sSQL));
		if ($this->getNumberOfRows() == 0) {
			return false;
		}
		$this->setResultSet($this->getCurrentResource());
		return true;
	}

	function getNumberOfRows()
	{
		$iRows = 0;
		$oResource = $this->getCurrentResource();
		if (@mysql_num_rows($oResource) > 0) {
			$iRows = mysql_num_rows($oResource);
		}
		
		return $iRows;
	}

	function selectInfo($sSQL, $bDebugMode = FALSE)
	{
		$aReturn = FALSE;
		$this->query($sSQL, $bDebugMode);
		if ($this->getNumberOfRows() > 0) {
			$aReturn = $this->getResultSet();
		}
		return $aReturn;
	}
	
	function lastId()
	{
		return mysql_insert_id();
	}

	function closeConnection()
	{
		mysql_close($this->oConn);
	}

	function createSQLString($sInput)
	{
		$sInput = "'" . addslashes($sInput) . "'";
		return $sInput;
	}

	function logToDatabaseViaArray($aFieldInfo, $sTableName, $sSQLConstraints = "", $sMethod = "INSERT")
	{
		$sFields = "";
		$sValues = "";
		
		if ($sMethod == "UPDATE") {
			$sSQL = "UPDATE ".$sTableName." SET [** UPDATES **] ".$sSQLConstraints;
			$sUpdateString = "";
			foreach($aFieldInfo as $sKey => $sValue) {
				if (is_string($sValue)) {
					$sValue = $this->createSQLString($sValue);
				}
				
				$sUpdateString .= $sKey . " = ".$sValue.", ";
			}
			$sUpdateString = substr($sUpdateString, 0, strlen($sUpdateString)-2);
			$sSQL = str_replace("[** UPDATES **]", $sUpdateString,$sSQL);
			$this->query($sSQL);
			
		} else {
			$sSQL = "INSERT INTO ".$sTableName." ([** FIELDS **]) VALUES ([** VALUES **])";
			
			foreach($aFieldInfo as $sKey => $sValue) {
				if (is_string($sValue)) {
					$sValue = $this->createSQLString($sValue);
				}
				
				$sFields .= $sKey . ", ";
				$sValues .= $sValue . ", ";
			}
			$sFields = substr($sFields, 0, strlen($sFields)-2);
			$sValues = substr($sValues, 0, strlen($sValues)-2);
			
			$sSQL = str_replace("[** FIELDS **]", $sFields,$sSQL);

			$sSQL = str_replace("[** VALUES **]", $sValues,$sSQL);
			$this->query($sSQL);
		}
	}

	function fetchResultsByQuery($sSQL, $bDebugMode = FALSE)
	{
		$this->query($sSQL, $bDebugMode);
		return $this->getResultSet();
	}

	function getInsertId()
	{
		return mysql_insert_id($this->oConn);
	}

	function destroyCurrentResource()
	{
		$oResource =& $this->getCurrentResource();
		if (is_object($oResource)) {
			mysql_free_result($oResource);
		}
	}

	// MUTATORS & ACCESSORS
	function setCurrentResource($oNewResource)
	{
		$this->oCurrentResource = $oNewResource;
	}
	function getCurrentResource()
	{
		return $this->oCurrentResource;
	}

	function setResultSet($oNewRecordSet)
	{
		$iLoop = 0;
		$this->aResultSet = array();
		
		while($ROW = mysql_fetch_array($oNewRecordSet, MYSQL_ASSOC)) {
			while(list($key,$val) = each($ROW) ) {
				$this->aResultSet[$iLoop][$key] = $val;
          	}
			$iLoop++;
		}
		if (count($this->aResultSet) > 0) {
			$this->setResultFound(TRUE);
		} else {
			$this->setResultFound(FALSE);
		}
	}
	function getResultSet()
	{
		if (isset($this->aResultSet)) {
			return $this->aResultSet;
		} else {
			return FALSE;
		}
	}
	function getResultRow($iRow=0)
	{
		if (isset($this->aResultSet)) {
			return $this->aResultSet[$iRow];
		} else {
			return FALSE;
		}
	}

	function setResultFound($bNewStatus)
	{
		$this->bResultsFound = $bNewStatus;
	}
	function getResultFound()
	{
		return $this->bResultsFound;
	}

} // END CLASS
?>