Click to See Complete Forum and Search --> : Ad Rotator with Admin Value


rlafountain
07-27-2005, 09:33 AM
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;
}
}
}

Weedpacket
07-27-2005, 11:38 PM
The total_ads() function could be simplified:

function total_ads()
{
$sql = 'select sum(*) from ads';
$result = mysql_query($sql);
if(!$result)
{
// Something nicer than the following
// would be preferable for real-world
// deployment
die(mysql_error());
}
return mysql_result($result,0);
}

rlafountain
07-28-2005, 10:24 AM
Yes I figured that out yesterday. Also, I think the random function needs to have a minimum of 1 and the low_num has to also start at 1.

Let me know your thoughts on this if you get a chance.

Thanks,
Ryan