Hello everyone. I have been looking long and hard for a very simple ad rotator with administrative value. So that the more weight you put on the value (the higher the number) the more often that ad is displayed. I believe I have come up with successful code and I was just wondering if anyone would take the time to either help me test it and/or provide me with some type of feedback on it.
Thank you,
Ryan
PHP Code:
function total_ads(){
$total = 0;
$sql = "SELECT * FROM ads WHERE 1=1";
$result = mysql_query($sql);
if (!$result){
echo mysql_error();
die;
}
while ($row = mysql_fetch_assoc($result)){
$admin_dist = $row['admin_value'];
$total = $total + $admin_dist;
}
return $total;
}
function pick_ad(){
$total = total_ads();
$low_num = 0;
$sql = "SELECT * FROM ads WHERE 1=1";
$result = mysql_query($sql);
$rand = rand(0, $total);
while ($row = mysql_fetch_assoc($result)){
$this_admin_dist = $row['admin_value'];
if ($rand >= $low_num && $rand < ($low_num + $this_admin_dist)){
return $row['adid'];
}else{
$low_num = $low_num + $this_admin_dist;
}
}
}