I found this small scrpt on the net that is used for avertising banners and is supposed to use a weight of 1-10 to show banners more frequent with a higher weight, below is the code, I don't understand the weight part enough, can someone that might understand it tell me if the weight really works to show it more often? Maybe you can explain how the weight works in this script? Appreciate any help


DB Table
-- Table structure for table `advertisements`
-- 

CREATE TABLE IF NOT EXISTS `advertisements` (
  `id` int(10) NOT NULL auto_increment,
  `code` text NOT NULL,
  `url` text NOT NULL,
  `weight` int(10) NOT NULL default '0',
  `clicks` int(10) NOT NULL default '0',
  `views` int(10) NOT NULL default '0',
  `email` text NOT NULL,
  `owner_name` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


<?php
require_once "../config/functions.inc.php";


// -- Start Banner Array --
$sql = "select * from advertisements order by id asc";
$results = executeQuery($sql);
//$prodlist = array ();
while ($row = mysql_fetch_assoc($results)){
	for ($x=1;$x <= $row[weight];$x++){
		$prodlist[] = $row[id];
	}
}
srand ((double) microtime() * 1000000);
$magicnumb = rand(0, count($prodlist)-1);
$magicnum = $prodlist[$magicnumb];

//get banner
$sql2 = "select * from advertisements where id = $magicnum";
$results2 = executeQuery($sql2);
while ($row2 = mysql_fetch_assoc($results2)){
	$add = $row2[views] + 1;
	//update views
	$sql3 = "update advertisements set views = $add where id = $row2[id]";
	executeQuery($sql3);
	// show banner
	print "<A HREF=\"banner/redirect.php?id=$magicnum\" TARGET=\"_blank\">";
	print $row2['code'];
}
?>

    It gives every advertisement $weight chances to be picked (out of N, where N is the sum of the weights of all of the advertisements). If one advertisement has a weight of 5 and another has a weight of 10, the second advertisement is twice as likely to be picked as the first.

    Walking through the program yourself by hand, with some simple test data, would give you a better insight into what it's actually doing.

      thanks for the insight, I just wanted to make sure the weight actually is doing something and is functional and worth keeping =) because I been planning to make a script liek this for days and I am completely lost on how to do a weight system

        Write a Reply...