Make Your Own Cool Drop Down Ad's - The admin.php script (contd.)
(Page 4 of 7 )
After adding a banner, a confirmation message will be displayed in the browser, linking back to the list of banners. By calling admin.php without any query string value for method, the ShowBanners function is called:
switch($_GET["method"])
{
// Other options here
default:
ShowBanners();
} ShowBanners simply connects to our MySQL database and retrieves the bannerID, title, numImps and numClicks field for each banner from the banners table and displays them in a list. The ShowBanners function also works out the click-thru rate for each banner as well. Here's what admin.php output after I added two banners to the database:
ShowBanners starts off by executing a SELECT statement against the banners table:
$result = @mysql_query("select bannerId, title, numImps, numClicks from banners order by title asc"); With the results from this select statement, a table containing rows of the banners is built. Dividing the number of clicks by the number of impressions and then multiplying that result by 100 works out the click-thru rate for each banner:
<?php
if($row["numImps"] > 0)
{
echo number_format((($row["numClicks"] / $row["numImps"])*100), 2);
}
else
{
echo "0";
}
?>% The click-thru rate of a banner is typically measured as a percentage compared to the number of impressions, so we use the number_format function to format the result with two decimal places.
If you noticed the screenshot of the ShowBanner function above, then you would've seen the "Remove" link next to each banner. The remove link points to admin.php?method=delete&bannerId=xxx, which means that the DeleteBanner function is called, passing in the bannerId query string as its only parameter:
function DeleteBanner($BannerId) DeleteBanner connects to the database and executes an SQL DELETE command against the banners table, removing the banner whose bannerId field is $BannerId:
if(@mysql_query("delete from banners where bannerId = $BannerId"))
{
// Query OK, banner has been deleted
?>
<h2>Banner Deleted</h2>
Your banner has been successfully deleted a banner from the database. Use
the link below to goto the list of banners.
<br><br>
<a href="admin.php">View Banners >></a>
<?php
}
else
{
// Query failed
?>
<h2>An Error Occured</h2>
An error occured while trying to delete the selected banner
database. Use the link below to try again.
<br><br>
<a href="javascript:history.go(-1)"><< Go Back</a>
<?php
}Next: Viewing, expanding and collapsing a banner >>
More HTML Articles
More By Tim Pabst