Your open source for Development and Design

PHP and dynamic URLs

Filed under: PHP — Adi @ 4:09 pm February 3, 2010

Whenever I make a new website now I always think about what the urls will need to be like. Most off the shelf CMS platforms will have a plug in or option to enables similar functionality.  But if your writing the site yourself you will want to be able to convert URL from post Id’s to post names to Urls and back again.

I have used many methods to do this in the past but recently I found two really useful built in PHP functions. urlencode() and urldecode().

Alternatives to using urlencode() would be writing your own function to convert all the url unfriendly symbols, slashes, spaces etc. into HTML entities or just replacing them with hyphens.

Again PHP has an built in function for everyhting.

Google Charts Example

Filed under: API's,PHP — Tags: , , , , — Adi @ 12:14 pm March 7, 2009

API’s are becoming more popular on the web as many of the big online companies such as Google, Yahoo and Facebook offer there own open source code for web developers to use freely and easily.

Google Charts Basics

To begin with I am going to go through how to use the Charts Basics API from Google. This is an extremely easy one to start off with as you don’t require an API key or to install anything, you don’t even need a dynamic server side script to get some nice results.

For this example I am going to jazz it up with a bit of PHP using an associative array but you can easily replace this with a database or XML feed.

To call a chart we use the query string linking to the API which will end up looking something like this.

<img src="http://chart.apis.google.com/chart?chs=250x100&amp;chd=t:10,80,35,10&amp;cht=lc&amp;chl=wk1|wk2|wk3|wk4"
alt="Sample chart" />

To generate this dynamicaly I used the following code

$arr = array();

$arr['Jan']['wk1'] = 10;
$arr['Jan']['wk2'] = 80;
$arr['Jan']['wk3'] = 35;
$arr['Jan']['wk4'] = 10;

$chart= 'http://chart.apis.google.com/chart?';
$chartsize = 'chs=250x100';
$type = "cht=lc";
$vals = 'chd=t:';
$keys = 'chl=';
$alt = "January";

foreach( $arr['Jan'] as $key => $value){
	$vals .= $value.',';
	$keys .= $key.'|';
	}

And Lastly to pull all the code together.

<img src="<? echo $chart;?>
<? echo $chartsize;?>
&amp;<? echo substr("$vals", 0, -1);?>
&amp;<? echo $type;?>
&amp;<? echo substr("$keys", 0, -1);?>"
alt="Sample chart" />

To develop this example further use the official documentation available here Google Chart Basic

Random Banner with PHP

Filed under: PHP — Tags: , , , , , — Adi @ 4:26 pm February 20, 2009

This is a quick tutorial which shows how easy it is to make a random banner script with PHP. We will use a PHP array of images which is populated by a given directory and select a random index from the array to be the banner.

For this example I have chosen to list the files from a directory as this will keep the script fairly dynamic without the need of a database. The script could be easily modified to use a database but we can come back to that later on if we need to.

To start off all you will need is a folder with several banners and a page to display them on.

$dir will be the directory you have created to grab the images from, it is important to make sure this folder only contains banners.
$banners will be the array of images found in the given folder.


 $banners = array();
  $dir = 'res/banners/';
  if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
  if($file != "." && $file != ".."){
  $banners[] = $file;
  }
  }
  closedir($handle);
  }

  $rand = array_rand($banners,1);
  echo "<img src='".$dir.$banners[$rand]."' alt='banner'/>";   

The code above uses a few handy built in PHP functions:
opendir()
readdir()
array_rand()
print_r()

Older Posts »

Switch to our mobile site