Re: Banner Ad Script

From: Date: Wed, 12 Jul 2000 03:19:13 +0000
Subject: Re: Banner Ad Script
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-6055@lists.php.net to get a copy of this message
Thanks for replying, but I'm afraid this isn't going to work without hanging every now and then. Do you have any suggestions on how I can select random banner ad(s) based on weight w/ one being the lowest? Currently, I'm using the following algorithm which works great if you only have one banner ad: What I have done in the past is place each banner into an array n number of times, where n is the banner weight. This way banners with higher weight appear in the array more times than banners with a lower weight, and thus have a higher chance of being selected. Note that when I have done this, I only store the index to the banner (unique ID) within the array. Andrew digi-FX ------------------------------- http://www.digi-FX.net ----- Original Message ----- From: Andrew <fisherres@msn.com> To: <php-general@lists.php.net> Sent: Tuesday, July 11, 2000 4:50 PM Subject: [PHP] Banner Ad Script > Hello, > > I'm working on a banner ad rotation script in PHP. The following is the > code I came up with; however, at times it exceeds the maximum execution time > and other times it works fairly fast. Any ideas... > > Code Sample: > <?php > class digiAds { > var $ad; > > function digiAds ($max = 4) { > global $mysql_host, $mysql_user, $mysql_pass, $mysql_db, $digiAds; > mysql_connect ($mysql_host, $mysql_user, $mysql_pass); > mysql_select_db ($mysql_db); > $result = mysql_query ("SELECT id, weight FROM digiAdst WHERE > enabled='yes'"); > while ($row = mysql_fetch_array ($result)) { > for ($i = 0; $i < $row["weight"]; $i++) > $ids[] = $row["id"]; > } > mysql_free_result ($result); > > $i = 1; > > // THE SCRIPT CHOKES HERE > while ($i != $max) { > mt_srand ((double) microtime () * 1000000); > $picked_ad = mt_rand (1, count ($ids)); > for ($i = 0; $i < count ($used_ads); $i++) { > if ($ids[$picked_ad] == $used_ads[$i]) > $used = true; > } > if (!$used) > $used = false; > if ($used == false) { > mysql_query ("UPDATE digiAdst SET views=views+1 WHERE > id='$ids[$picked_ad]'"); > $result = mysql_query ("SELECT img, width, height FROM > digiAdst WHERE id='$ids[$picked_ad]'"); > $row = mysql_fetch_array ($result); > mysql_free_result ($result); > $this->ad[] .= "<a href=/digi-ads/clicks.php?id=" . > $ids[$picked_ad] . " target=_blank><img src=" . $digiAds["path"] . > $row["img"] . " width=" . $row["width"] . " height=" . > $row["height"] . " > border=0></a>"; > $used_ads[] = $ids[$picked_ad]; > $i++; > } > } > mysql_close (); > } > } > ?> > > > > BTW, I call the script as so: > Code Sample: > <table width=500 border=0 cellspacing=2 cellpadding=2 align=center> > <tr> > <?php $buttons = new digiAds (4); ?> > <td width=25% align=center><?php echo $buttons->ad[0]; ?></td> > <td width=25% align=center><?php echo $buttons->ad[1]; ?></td> > <td width=25% align=center><?php echo $buttons->ad[2]; ?></td> > <td width=25% align=center><?php echo $buttons->ad[3]; ?></td> > </tr> > </table> > > Andrew > digi-FX > ------------------------------- > http://www.digi-FX.net > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > >

« previous php.general (#6055) next »