PHP Weighted Text Link Script

One interesting way to sell text links is to weight the links. An advertiser can buy a link that will display in the same spot some percentage of the time, whether it be 33% or 100%. This means you can have multiple links for the same “spot”, but only one will be displayed at a time. This is a very simple PHP script that demonstrates this, it does not have any error checks in it, so I wouldn’t recommend using it for anything other than learning or testing.

Eventually I may release a full-fledged MySQL driven weighted link script that will have administration panels and all that good stuff. I won’t promise anything, though.

If you find any errors please let me know in the comments.

<?php
	/*
		Weighted Links By Jeremy Steele
		http://www.nusuni.com
		
		This is a simple script that will display links in various link "slots" depending on the links' weights.
		
		It randomly generates a number between 0 and 99 (including 0 and 99, so there are 100 possible choices) 
		for each link "slot". Each slot can have multiple possible links, which is the beauty of this script. 
		You can weigh the various links in each slot to make one appear more than another... etc. 
		
		This script uses a simple system. The weight of the link is a percentage of how often the link should appear for a specific spot.
		If you want it to appear 25% of the time, then type "25". 

		The script compares the randomly generated number and the link weights and figures out which link should appear. 
	*/
	
	class paid_link {
		var $url; //link URL
		var $title; //link anchor text
		var $desc; //link title attribute
		var $weight;
		
		function paid_link($url,$title,$desc,$weight) {
			$this->url = $url;
			$this->title = $title;
			$this->desc = $desc;
			$this->weight = $weight;
		}
	}

	$link_spots = array();
	
	/*
	link_spots[spot][link_index]
	*/
	
	$link_spots[0][0] = new paid_link("http://www.google.com","Google","Best SE",50);
	$link_spots[0][1] = new paid_link("http://www.yahoo.com","Yahoo","Go Yahoo!",50);
	
	$link_spots[1][0] = new paid_link("http://www.msn.com","MSN","Boo MSN",33);
	$link_spots[1][1] = new paid_link("http://www.newgrounds.com","Newgrounds","Newgrounds Rocks!",33);
	$link_spots[1][2] = new paid_link("http://www.videogamedc.com","Video Game DC","Pixely Cartoons, Always Fun!",33);
	
	$link_spots[2][0] = new paid_link("http://www.nusuni.com","Nusuni","Nusuni.com Rules",100);
	
	$link_spots[3][0] = new paid_link("http://www.apple.com","Apple","Apple makes the best computers in the world!",25);
	$link_spots[3][1] = new paid_link("http://www.microsoft.com","Microsoft","Microsoft.... blah",25);
	$link_spots[3][2] = new paid_link("http://www.dell.com","Dell","Dell.... blah",25);
	$link_spots[3][3] = new paid_link("http://www.hp.com","HP","HP is Okay...",25);
	
	$link_spots[4][0] = new paid_link("http://www.imdb","IMDB","Internet Movie Database",100);
	
	function find_link($spot,$num) {
		global $link_spots;
		
		$spot = &$link_spots[$spot]; //pointer to link spot array
		$curnum = -1; //we start at -1, becuase of the way the math works
		$curlink = -1; //same here
		
		
		/*
		This loop will go through all the link spots. If a link fitso ut criteria the index is stored and we continue looping, 
		just to make sure no other links would work.
		*/
		for($i = 0;$i < count($spot);$i++) {	
			if($num > $curnum) {
				$curlink = $i; //set the curlink var
			}
			
			$curnum += $spot[$i]->weight;
		}
		
		return $curlink;
	}
	
	function display_link($spot) {
		global $link_spots;
		
		$random = rand(0,99);
				
		$num = find_link($spot,$random); //find out link
						
		$link = &$link_spots[$spot][$num]; //get a pointer to the link
			
		print("<a href=\"" . $link->url . "\" title=\"" . $link->desc . "\">" . $link->title . "</a><br />");
	}
	
	display_link(0);
	display_link(1);
	display_link(2);
	display_link(3);
	display_link(4);
?>

Please subscribe, or else I will cry. Do you really want to make a programmer cry?

Leave a Reply

Note: By submitting your comment you agree to this blog's comment policy.

If you want a little icon next to your name - sign up for one at Gravatar.