Banner Ad Script
| From: | Andrew | Date: | Tue, 11 Jul 2000 23:50:58 +0000 |
| Subject: | Banner Ad Script | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-6039@lists.php.net to get a copy of this message | ||
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