PHP Ad Tracker: Entering Ad Banner and Client Data

In our last session, we showed how to list the ad banner data and client data.  In this session, we will examine the form that allows administrators to enter ad banner data.

First, we include the class_ads.php file and instantiate the ads class:

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

// instantiate ads class
$oAds = new ads;

Next, we assign a value for the $id variable by suing the setID function in the ads class:

// check for id
if ($id) {

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

If the form has posted data, we assign values to each of the variables according to the data posted in the form fields:

if ($_POST) { // if form was posted

// assign page variables
$iClientId = $_POST["clientid"];
$sTitle = $_POST["title"];
$sUrl = $_POST["url"];
$sPath = "";
$sClient = $_POST["client"];
$sContact = $_POST["contact"];
$sEmail = $_POST["email"];
$sPhone = $_POST["phone"];

We can also check if the ad title is no longer than 200 characters using a separate validInput function:

// validate ad title
if (!validInput($sTitle, 1, 200)) {

catchErr("Enter a valid ad title");
$FORMOK = false;
}

We can also check if the ad URL has a valid format using a separate validURL function:

// validate ad url
if (!validUrl($sUrl)) {

catchErr("Enter a advertisement URL");
$FORMOK = false;
}

We can also check if the file has been properly uploaded with the is_uploaded_file function and that the file has a valid name and extension with a separate validFile function. If the file does not have a valid name, we can assign a name by a combination of a random number and the current date:

// check for uploaded file
if (is_uploaded_file($_FILES["banner"]["tmp_name"])) {

// get file extension
if (!$sExt = validFile("banner")) {

$FORMOK = false;

} else {

// generate random unique file name
$iRand = rand(100001, 999999);
$sImgName = md5(strtotime(date("Y-m-d H:i:s")).$iRand).".".$sExt;
$sPath = SITE_URL."/_img/_banners/".$sImgName;
}

} else { // file not uploaded

// if the operation is add catch an error
if (!strcmp("add", $op)) {

catchErr("Upload a banner image file");
$FORMOK = false;
}
}

Once the file data has been validated, we can also validate the client information with the same functions.

// validate client id
if ($iClientId < 1 && !strcmp("add", $op)) {

// validate client name
if (!validInput($_POST["client"], 1, 100)) {

catchErr("Enter a client name");
$FORMOK = false;
}

// validate client contact
if (!validInput($sContact, 1, 100)) {

catchErr("Enter a client contact name");
$FORMOK = false;
}

// validate client contact email
if (!validEmail($sEmail)) {

catchErr("Enter a client contact email address");
$FORMOK = false;
}

// validate client contact phone
if (!validInput($sPhone, 1, 20)) {

catchErr("Enter a client contact phone number");
$FORMOK = false;
}

} elseif ($iClientId < 1 && !strcmp("edit", $op)) {

catchErr("Select a client");
$FORMOK = false;
}

After the client data has been validated, we can assign the data to elements in an array:

if ($FORMOK) { // form validated ok

// assign item values
$aArgs["Client Id"] = $iClientId;
$aArgs["Title"] = $sTitle;
$aArgs["URL"] = $sUrl;
$aArgs["Path"] = $sPath;
$aArgs["Client"]["Name"] = $sClient;
$aArgs["Client"]["Contact"] = $sContact;
$aArgs["Client"]["Email"] = $sEmail;
$aArgs["Client"]["Phone"] = $sPhone;

If the query string attached to the form input specifies that the data needs to be edited, the page calls the editAd function and replaces the ad data in the ad table:

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

// try edit ad
$FORMOK = $oAds->editAd($aArgs);

If the query string attached to the form input specifies that the data needs to be added, the page calls the addAd function and adds a new row in the ad table:

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

// try add ad
$FORMOK = $oAds->addAd($aArgs);
}

In either case, the code will copy the ad banner images to the banners directory and redirect the user to the listing page:

// redirect if successful
if ($FORMOK) {

// copy file
copy($_FILES["banner"]["tmp_name"], "../../_img/_banners/".$sImgName);

// redirect if successful
header("Location: index.php");
}
}

If the form has not posted data, the form will retrieve the data for the selected ad: with the getAd function and assign the values to the form fields:

} else {

// initialize page variables
if (!strcmp("edit", $op)) {

// get ad
$aAd = $oAds->getAd();

// initialize page variables
$iClientId = $aAd["Client Id"];
$sTitle = $aAd["Title"];
$sUrl = $aAd["URL"];
$sClient = $aAd["Client"]["Name"];
$sContact = $aAd["Client"]["Contact"];
$sEmail = $aAd["Client"]["Email"];
$sPhone = $aAd["Client"]["Phone"];
}
}
?>
SHARE THIS POST
  • http://www.searcheccentric.com/seo-services.html Tom

    Great post, I find many important stuff at your blog.

  • http://www.facebook.com/pablofmorales Pablo Morales

    what the fuck it’s that shit?