PHP Ad Tracker: Listing Ad Banners and Clients

In our last session, we finished writing the PHP class file that would control all the ad banner functions.  This week, we look at the presentation layer for the administration of these functions.

The ads_list.php page lists the current banner ads.  First, we must include the class file so that the page can instantiate the class call the functions:

<?php
require_once("class.ads.php");

// instantiate ads class
$oAds = new ads;

Next, we fill the $aAds array with the data from the ads table and get a count of the records:

// get users and user count
$aAds = $oAds->getAds("created_dt desc", $iCursor);
$iCnt = $oAds->getAdsCount();

// check for users
if (count($aAds)) {

// build page data array
$i = 0;
while ($i < count($aAds)) {
$aData[$i]["Id"] = $aAds[$i]["Ad Id"];
$aData[$i]["Name"] = $aAds[$i]["Title"];
$aData[$i]["Status"] = $aAds[$i]["Status"];
$aData[$i]["Created"] =$aAds[$i]["Created Date"];
++$i;
}
}

The next lines check for an ad ID number and, if one exists, assigns that ID number to the $oAds variable.

// check for id
if ($id) {

// assign unique id
$oAds->setId($id);

After the script assigns the ID number, it also checks for any query strings that call for specific operations (delete, activate, deactivate) :

// check operation type
if (!strcmp($op, "del")) {

// try delete ad and redirect
$oAds->deleteAd();
header("Location: ".SELF);

} elseif (!strcmp($op, "act")) {

// try activate ad and redirect
$oAds->activateAd();
header("Location: ".SELF);

} elseif (!strcmp($op, "deact")) {

// try deactivate ad and redirect
$oAds->deactivateAd();
header("Location: ".SELF);
}
}

?>

The HTML section displays a list of the ads: (NOTE: You can write your own custom HTML to create the page headers, meta tags and body framework.)

<table width="608" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2">Developer Drive Advertisements Administration</td>
</tr>
<tr>
<td colspan="2"> Choose an ad banner and an action from the list below.</td>
</tr>
<tr>
<td><?php writeErrors() ?></td>
<td align="right" valign="top">
<?php if ($iPerm > 1) { ?>
<a href="form.php?op=add"><img src="../images/btn_additem.gif" alt="Add Banner" border="0" /></a>
<?php } ?>
</td>
</tr>
</table>

<?php renderList($iCnt, $aData) ?>

The clients_list.php works in much the same way. You must include the class file in order to instantiate the class:

<?php

require_once("class.ads.php");

// instantiate ads class
$oAds = new ads;

The next step is to create the $aClients array variable and the $iCnt integer variable. The $aClients variable will hold the client information and the $iCnt variable will hold the number of records in the clients table.

// get users and user count
$aClients = $oAds->getClients("created_dt desc", $iCursor);
$iCnt = $oAds->getClientsCount();

// check for users
if (count($aClients)) {

// build page data array
$i = 0;
while ($i < count($aClients)) {
$aData[$i]["Id"] = $aClients[$i]["Client Id"];
$aData[$i]["Name"] = $aClients[$i]["Client"];
$aData[$i]["Status"] = $aClients[$i]["Status"];
$aData[$i]["Created"] =$aClients[$i]["Created Date"];
++$i;
}
}

If the $id variable has a non-null value, then the page will set the $oAds variable to that value. The page will also act on specific values of a query string to call correlating functions of the class (delete, activate, deactivate).

// check for id
if ($id) {

// assign unique id
$oAds->setId($id);

// check operation type
if (!strcmp($op, "del")) {

// try delete client and redirect
$oAds->deleteClient();
header("Location: ".SELF);

} elseif (!strcmp($op, "act")) {

// try activate client and redirect
$oAds->activateClient();
header("Location: ".SELF);

} elseif (!strcmp($op, "deact")) {

// try deactivate client and redirect
$oAds->deactivateClient();
header("Location: ".SELF);
}
}

?>

The HTML table displays a list of the clients:

<table width="608" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><div>Developer Drive Clients Administration</div></td>
</tr>
<tr>
<td colspan="2"><div> Choose an ad client and an action from the list below.</.</div></td>
</tr>
<tr>
<td><div>
<?php writeErrors() ?>
</div></td>
<td align="right" valign="top">
<?php if ($iPerm > 1) { ?>
<a href="form.php?op=add">
<img src="../images/btn_additem.gif" width="58" height="15" alt="" border="0" /></a>
<?php } ?>
</td>
</tr>
</table>

<?php renderList($iCnt, $aData) ?>

In next week’s lesson, we will examine the data entry forms for the ads and clients tables.

Gerald Hanks has been involved in web development applications since 1996. He has designed applications with JavaScript, ASP.NET and PHP, as well as building databases in MS SQL Server and MySQL. He lives in Houston, Texas. More articles by Gerald Hanks
Home CSS Deals DesignBombs HTML HTML5 JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress