Month: September 2011

PHP Modulo Operator For Alternating Rows

Alternating row colours within a grid of data can make for a more pleasant viewing experience and has become a popular styling method in recent times. I was a little incredulous at some code I saw recently to determine alternating rows so without further introduction, meet the Modulo operator which is a quick and easy way to get up and running:

for($i=0;$i<10;$i++){ if($i % 2){ // This is an even row }else{ // This is an odd row } }

I'm sure you will be able to apply this to your own css or styling within your loops.

Use cURL to obtain remote filesize

Sometimes you might want to determine a remote file size prior to determining further action to take. This is a quick function that utilises cURL to retrieve the file size in bytes.
function remoteFilesize ($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_exec($ch);
$info = explode('Content-Length:', $info);
$info = explode("Connection",$info[1]);
return $info[0];

Usage is simple:

echo remoteFilesize("");

Get Facebook Likes and Shares via Graph API

Facebook Like and Share count can easily be obtained from the Facebook Graph API. This is an extremely quick way to look up the JSON response from Facebook so that you can further process it for your own requirements.
$URL ='';
$JSON = file_get_contents($URL);
$Output = json_decode($JSON);
$Likes = 0;
  $Likes = $Output->likes;

echo "Number of likes for CocaCola's Facebook Page = ".$Likes."</br>";

$URL2 ='';
$JSON2 = file_get_contents($URL2);
$Output2 = json_decode($JSON2);
$Shares = 0;
  $Shares = $Output2->shares;

echo "Number of shares for = ".$Shares;

UPDATE 29/01/2012: I’ve written a slightly different method using the PHP SDK in this post here

Currency Conversion using Yahoo Finance API

Just a quick and dirty PHP function to retrieve information from the Yahoo Finance API to perform a currency conversion
function ConvertCurrency($From,$To,$Amount){
$URL = ''. $From . $To .'=X';
$Handle = @fopen($URL, 'r');

if ($Handle) {
$Result = fgets($Handle, 4096);
$allData = explode(',',$Result);
$Value = $allData[1];

return $Value * $Amount;

$Amount = ConvertCurrency("NZD","USD",25);

Using mod_rewrite to remove variables from a URL

The Apache mod_rewrite module offers an unparalleled toolset for manipulating URLs, – however configuration can be a tricky beast at times. While most people are comfortable enough removing file extensions like .php we recently had a user who wanted to hide all the variables in the address string.

For example index.php?id=id would end up being index/id
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about/(.*)$ index.php?id=$1 [QSA]

The magic here is the QSA or Query String Append. If you just wanted to remove the .php you would use
RewriteCond %{REQUEST_URI} !.*\.php$
RewriteRule ^/(.*)$ $1.php [QSA]