Re: Less Complex Way
| From: | Matthew | Date: | Tue, 04 Jul 2000 01:21:11 +0000 |
| Subject: | Re: Less Complex Way | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-4628@lists.php.net to get a copy of this message | ||
Well, you're whole if/then/else structure could be simplified like this:
// _MAX_ = 11 (or whatever)
if ($row['weight'] > 0 && $row['weight'] < _MAX_)
$mynum = _MAX_ - $row['weight'];
else
$mynum = 0;
I didn't look at your code in much detail, so I'm not sure exactly
what you're trying to accomplish. :) But the above should be quicker,
and easier to expand. It'll work just as well as what you have
written below.
--
Matthew Leverton - matthew@aeroinc.net
http://www.allegro.cc
-----Original Message-----
From: Nick Catalano <nick4753@nozone.net>
To: PHP_LIST Genaral <php-general@lists.php.net>
Date: Monday, July 03, 2000 7:02 PM
Subject: [PHP] Less Complex Way
>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);
>}
>
>