Wednesday, August 31, 2011

Dynamic meta Description, meta Keywords and Title using PHP

Changing your the meta description, meta keywords and title of a php page.This is a follow up of a post that I had posted here a few weeks back.


I only make some changes to the header.php file and the header.php file will handle the value of $section and change the title, the meta description and meta keywords of the webpage.
<?
	//Conditional Statement to change the title, meta description and meta keywords.
	if ($section == "home"){
		$title = "Unique title for home page";
		$description = "A Unique description for 'Home' Section";
		$keyword = "Home";
	}else if ($section == "profile"){
		$title = "Special title for profile page";
		$description = "A Special description for 'Profile' Section";
		$keyword = "Profile";
	}else if ($section == "product"){
		//Another Conditional Statement to change the title, meta description and meta keywords base on the productID.
		$productID = $_REQUEST["productID"];
		if($productID == "x"){
			$title = "Product page for X";
			$description = "A description for 'Product X'";
			$keyword = "Product X";
		}else if($productID == "y"){
			$title = "Product page for Y";
			$description = "A description for 'Product Y'";
			$keyword = "Product Y";
		}else if($productID == "z"){
			$title = "Product page for Z";
			$description = "A description for 'Product Z'";
			$keyword = "Product Z";
		}else{
			$title = "A title for product page";
			$description = "A description for 'Product' Section";
			$keyword = "Product X, Product Y, Product Z";
		}
	}else if ($section == "contact"){
		$title = "Contact me now";
		$description = "Form for contact me";
		$keyword = "Conact me, form";
	}
	//Write the title, meta description and meta keywords into a header html tag
	echo "<header>";
	echo "<title>".$title."</title>";
	echo "<meta name='description' content='".$description."'>";
	echo "<meta name='keywords' content='".$keyword."' />"; 
	echo "</header>";
	//Create a table and base on the value os $section, toggle the state of the selected button.
	echo "<table cellpadding='0' cellspacing='0' border='0' width='100%' ><tr>";
	//If $section == home, make the text 'Home' unclickable else makt it into a clickable hyperlink
	if ($section != "home"){
		echo "<td align='center'><a href='home.php'>Home</a></td>";
	}else{
		echo "<td align='center'><b>Home</b></td>";
	}
	//If $section == profile, make the text 'Profile' unclickable else makt it into a clickable hyperlink
	if ($section != "profile"){
		echo "<td align='center'><a href='profile.php'>Profile</a></td>";
	}else{
		echo "<td align='center'><b>Profile</b></td>";
	}
	//If $section == product, make the text 'Product' unclickable else makt it into a clickable hyperlink
	if ($section != "product"){
		echo "<td align='center'><a href='product.php'>Product</a></td>";
	}else{
		echo "<td align='center'><b>Product</b></td>";
	}
	//If $section == contact, make the text 'Contact Us' unclickable else makt it into a clickable hyperlink
	if ($section != "contact"){
		echo "<td align='center'><a href='contact.php'>Contact Us</a></td>";
	}else{
		echo "<td align='center'><b>Contact Us</b></td>";
	}
	echo "</tr></table>";
	echo "<br>";
?>

Click here for the example that I had created for the post.
Try click and copying the following URL1 and URL2 into Facebook and you will see how useful php can be.
Click here for the source codes of the example that I had created.

Friday, August 26, 2011

COMEX 2011

It's time to get ready for COMEX 2011. Time to catch a glimpse of the latest gadgets and at the same time get all the gadget at a promotional price. :P



Image taken form the COMEX website.

Details of COMEX 2011:
  • Venue: Level 1,2,3,4 and 6, Suntec City Convention Hall, Singapore
  • Date: 1st - 4th September 2011
  • Opening hours: 12noon - 9pm

* Click here for the website of COMEX 2011.

Wednesday, August 24, 2011

Flex: Measuring width of text of a Label

I have encounter a few situations in Flex where you need to position 2 labels side by side and the one on the left always ended up to be a dynamic label. If this is Flash, you can always use a .textWidth / .textHeight to solve the problem but in Flex, some of these properties were not available hence here's a workaround for it.



	
	
		Label{
			font-Size:20;
		}
	
	
		
	
	
	
		
		
			
		
		
			
		
		
			
		
		
			
		
		
		
			
			
		
	


Click here to view the demo.
Click here to download the source file of the demo.

Monday, August 22, 2011

Google Plus - Games Notification

Google Plus+ just added a new section for 'Games' recently. And as compared to the way how Facebook had organised the way game notifications were displayed, I love the way Google is displaying all the game notifications.


This is how Google Plus+ is doing it and I like it. Rather then messing up the feeds of my friends, they have created a seperate section for all these annoying 'Games Notifications'. This my friend is a +1 indeed. Though I still want a -1 button from Google Plus+. Can anyone help me to pass the message to Google asking them for a -1 button?

* Click here to access Google Plus+ website.
^ Click here to access Facebook.

Sunday, August 14, 2011

Flash / Flex AS3: Finding the difference between 2 dates

I have encounter numerous situation where there is a need to compare 2 different dates. I have created a simple example to demonstrate how to find out the difference between 2 dates.



	
		Days: " + tempArray[0] + "";
				outputTxt.htmlText += "
Hours: " + tempArray[1] + ""; outputTxt.htmlText += "
Minutes: " + tempArray[2] + ""; outputTxt.htmlText += "
Seconds: " + tempArray[3] + ""; outputTxt.htmlText += "
MilliSeconds: " + tempArray[4] + ""; } ]]>

package
{
	import mx.controls.DateField;
	import mx.controls.TextArea;

	public class TimeDifferenceMain
	{
		public function TimeDifferenceMain()
		{
		}
		
		public function compareDate(tempDate:Date):Array
		{
			//2 dates object: 1 is today's date, the other 1
			//is the selected date
			var now:Date = new Date();
			var selectedDate:Date = tempDate;
			
			//to store the difference of both dates in numerical value
			var remainder:Number;	
			
			//Store the number of milliseconds of both date since 1970
			var nowNum:Number = now.getTime();
			var selectedDateNum:Number = selectedDate.getTime();
			
			//Numbers to store number of milliseconds in eash day, 
			//each hour, each minute and each second
			var dayValue:Number = 24 * 60 * 60 * 1000;
			var hrValue:Number = 60 * 60 * 1000;
			var minValue:Number = 60 * 1000;
			var secValue:Number = 1000;
			
			//Compare which date is bigger and find the difference
			if(nowNum > selectedDateNum)
			{
				remainder = nowNum - selectedDateNum;	
			}else{
				remainder = selectedDateNum - nowNum;
			}
			
			//Modulas play a big role here as it is used to find
			//out the remainder of the next smaller unit
			var hrLeft:int = remainder % dayValue;
			dayValue = (remainder - hrLeft)/dayValue;
			var minLeft:int = hrLeft % hrValue;
			hrValue = (hrLeft - minLeft)/hrValue;
			var secLeft:int = minLeft % minValue;
			minValue = (minLeft - secLeft)/minValue;
			var milliSecLeft:int = secLeft % secValue;
			secValue = (secLeft - milliSecLeft)/secValue;
			
			//Create a array and store the output into the array
			var resultArray:Array = new Array();
			resultArray.push(dayValue);
			resultArray.push(hrValue);
			resultArray.push(minValue);
			resultArray.push(secValue);
			resultArray.push(milliSecLeft);
			return resultArray;
		}
	}
}

Click here to view the demo.
Click here to download the source file of the demo.

Sunday, August 7, 2011

Facebook: Changing the description of your websites / blogs

This happen to me quite often in the past. I was building a website or after I had written a new post on one of my blogs, I would immediately copy and paste the URL of the website onto my Facebook wall. Upon doing that, Facebook will show you the title, the description and a series of thumbnail for you to select of the URL that you have pasted. You realise that some parts of the copy were missing due to some illegal characters or you suddenly decided to change the thumbnail of the website. After making that minor change, Facebook will still reflect the same contents as per what you had before you make the minor change. What to do? How to work around this issue?

As long as the same URL have not been shared by any other users, you will still have a way to ask Facebook to update to the latest content.

Click here to move to one of the developer tools that Facebook had created.

Copy and paste the URL that you want to share into the text field and click on the 'Lint' button located next to the text field.

Now, the updated content should be appearing on this webpage and you can start sharing your URL with the correct set of contents now. :)

Note:
  • This method will not work after someone had already started sharing the same URL that you are sharing.
  • If this method doesn't work, create a separate server script scripting file and do a redirect from that file. And share the new URL. :D

Sunday, July 31, 2011

Why choose PHP?

There were many types of server side scripting environment out there. For example: there's php, asp, asp.net etc. But I choose php most of the time because its free and it's easy to use.


Let's look at the scripts below:
I am using a flash file to communicate to the server side script.
package com.phppostget
{
 import com.zcs.net.NetConnect;
 
 import flash.display.MovieClip;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import flash.net.NetConnection;
 import flash.net.URLVariables;
 import flash.text.TextField;
 
 public class Main extends Sprite
 {
  //This is a class that I had written to simplify 
  //the connection to a server side script
  private var tempConnect:NetConnect;
  //Create pointers to the movieclips and textfield on Stage
  public var _send_mc:MovieClip = new MovieClip();
  public var _btn_mc:MovieClip = new MovieClip();
  public var _value1_txt:TextField = new TextField();
  public var _value2_txt:TextField = new TextField();
  public var _value3_txt:TextField = new TextField();
  public var _input1_txt:TextField = new TextField();
  public var _input2_txt:TextField = new TextField();
  public var _input3_txt:TextField = new TextField();
  public var _url_txt:TextField = new TextField();
  public var _result_txt:TextField = new TextField();
  
  public function Main(){
   //Setup all the pointers to the respective movieclips and textfields
   //and setup the listeners to all these Movieclips and buttons
   _btn_mc = this.getChildByName("btn_mc");
   _btn_mc.gotoAndStop(1);
   _btn_mc.addEventListener(MouseEvent.CLICK, changeMethodEvent);
   _send_mc = this.getChildByName("send_mc");
   _send_mc.addEventListener(MouseEvent.CLICK, sendDataEvent);
   _value1_txt = this.getChildByName("value1_txt") as TextField;
   _value2_txt = this.getChildByName("value2_txt") as TextField;
   _value3_txt = this.getChildByName("value3_txt") as TextField;
   _input1_txt = this.getChildByName("input1_txt") as TextField;
   _input2_txt = this.getChildByName("input2_txt") as TextField;
   _input3_txt = this.getChildByName("input3_txt") as TextField;
   _url_txt = this.getChildByName("url_txt") as TextField; 
   _result_txt = this.getChildByName("result_txt") as TextField; 
  }
  
  //Simple listner to toggle the POST and GET state 
  //upon clicking on the Movieclip
  private function changeMethodEvent(event:Event):void{
   if(_btn_mc.currentFrame == 1){
    _btn_mc.gotoAndStop(2);
   }else{
    _btn_mc.gotoAndStop(1);
   }
  }
  
  //Sending the values to the server side script
  //upon clicking on the Send button
  private function sendDataEvent(event:Event):void{
   var tempXMLStr:String = "<"+_input1_txt.text+">" + _value1_txt.text + "";
   tempXMLStr += "<"+_input2_txt.text+">" + _value2_txt.text + "";
   tempXMLStr += "<"+_input3_txt.text+">" + _value3_txt.text + "";
   var tempXML:XML = new XML(tempXMLStr);
   
   tempConnect = new NetConnect(_url_txt.text, tempXML, Number(_btn_mc.currentFrame));
   tempConnect.addEventListener(NetConnect.PARSE_NET_OK, showResultEvent);
  }
  
  //When the server side script returns a result, show it on the result textfield
  private function showResultEvent(event:Event):void{
   if(_btn_mc.currentFrame == 1){
    _result_txt.text = "POST: ";
   }else{
    _result_txt.text = "GET: ";
   }
   var urlVar:URLVariables = new URLVariables(tempConnect.getResult());
   _result_txt.appendText(urlVar.msg);
  }
 }
}

Look at the source code of this php file. I don't need to bother about the calls to this php using a POST or a GET method. $_REQUEST will handle both POST and GET method calls. Isn't that great?
<?php
  //Regardless of POST or GET, grab the values that 
  //were send to this php files
  $name = $_REQUEST["name"];
  $desc = $_REQUEST["desc"];
  $info = $_REQUEST["info"];
  //output a simple message with the values that were 
  //pass into this php file
  echo "msg=Hello ".$name.", the following are your description:".$desc." and here are your info.".$info."&misc=";
?>

Click here for the demo of this post.
Click here for the source files of this post.