Commercial Break (A phpAds Primer) - The Advanced Course (
Page 6 of 9 )
You'll remember that only the first argument to view() is compulsory -
all the rest are optional. However, these optional arguments can come in pretty
handy at times. For example, let's suppose you have a client who's paid you a
premium to display only his banners on a particular page of your site. However,
the view() function, by itself, displays a random customer's banner each time it
is invoked. Not a Good Thing at all.
You can modify this default
behaviour of the view() function by passing it a second argument, a client ID.
This forces view() to only displays banners belonging to that client when it is
invoked, and can come in handy when you want to mark certain pages as exclusive
to particular customers. Here's an example, which only displays banners
belonging to customer ID 21.
<? view("teenagers", 21); ?>
By default, clicked banners open up in the same browser
window - which may not be acceptable to you. You can use the third argument to
view() to specify a different target for clicks - as in the following example,
which launches the target of each banner in the window named "ad".
<? view("teenagers", 21, "ad"); ?>
The fourth argument to view() is a Boolean, which allows you
to hide or show the optional text under the banner. By default, the text display
is turned off; you can turn it on with something like this.
<? view("teenagers", 21, "ad", true); ?>
The last optional argument to view() is probably also the
most complex - it is used as a filter to restrict the banners displayed. It
consists of an array, which in turn consists of one or more associative arrays.
Each of these associative arrays contains a key - which must be either the
equality operator == or the inequality operator != - and a value, which must be
a banner ID. This series of nested arrays is used by phpAds to provide very
focused control over which banners can appear on which pages.
Consider
the following example, which displays two banners on the same page. Since the
selection of banners is random, it's possible that the same banner could be
repeated in both places. You can avoid this by creative use of the banner
filter, as in the example below.
<?
require("phpAds/config.inc.php3");
require("phpAds/view.inc.php3");
require("phpAds/acl.inc.php3");
?>
<html>
<head>
<basefont face="Arial">
</head>
<body>
<table height="100%" width="100%" border="0" cellspacing="5"
cellpadding="5"> <tr>
<td align="center"><? $id = view("studio"); ?></td>
</tr>
<tr>
<td height="100%" width="100%" valign="top" bgcolor="silver"
align="left">
<h2>Sample page</h2>
<center>
Content here
<p>
Content here
<p>
Content here
<p>
</center>
</td>
</tr>
<tr>
<td align="center"><? view("studio", NULL, NULL, true,
array(array("!=" => $id))); ?></td> </tr>
</table>
</body>
</html>
In this case, the first call to view() returns a banner whose
ID is stored in the PHP variable $id. The second call to view() then uses this
$id variable, in combination with an inequality operator, to ensure that the
same banner does not get repeated.
You can reverse things so that the
same banner appears at both the top and bottom of the page, by using the
equality operator instead of the inequality operator in the example above. Try
it yourself and see.