Less Complex Way

From: Date: Tue, 04 Jul 2000 00:03:11 +0000
Subject: Less Complex Way
Groups: php.general 
Request: Send a blank email to php-general+get-4609@lists.php.net to get a copy of this message
Ok, I posted before and got a reply that said to create a array to find a random number using a number's weight... I created this script (yet to be tested) to find the ad ($aid) according to the weight (I am createing a free advertising rotation service using PHP, oh fun)... this script seems sorta complex, and I want to make this script as fast as possible, so can someone take a look at this script and see if there is anything that can be changed to change the speed of the script.... The Function: function pick_banner ($username, $account, $zone) { $num = 0; $result = mysql_query("select weight,aid from banners WHERE username = '$username' AND account = '$account' AND zone = '$zone' OR zone = 'zoneless_banner'"); while($row = mysql_fetch_array($result)) { if ($row['weight'] == "10") { $mynum = 1; } else if ($row['weight'] == "9") { $mynum = 2; } else if ($row['weight'] == "8") { $mynum = 3; } else if ($row['weight'] == "7") { $mynum = 4; } else if ($row['weight'] == "6") { $mynum = 5; } else if ($row['weight'] == "5") { $mynum = 6; } else if ($row['weight'] == "4") { $mynum = 7; } else if ($row['weight'] == "3") { $mynum = 8; } else if ($row['weight'] == "2") { $mynum = 9; } else if ($row['weight'] == "1") { $mynum = 10; } else if ($row['weight'] == "0") { $mynum = 0; } else { $mynum = 0; } for ($a = $num; $a <= $mynum; $a++) { $array[$a] = $aid; $num = $a; } } mysql_free_result($result); srand((double)microtime()*time()); $ad = rand(0, $num); return($ad); }

« previous php.general (#4609) next »