Affiliate System
1)First we need to create the table in our mysql database to allow us to store our affiliates information.
SQL query:
CREATE TABLE `affiliates` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`url` VARCHAR( 100 ) NOT NULL ,
`banner` VARCHAR( 100 ) NOT NULL ,
`approved` TINYINT( 1 ) DEFAULT '0' NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM ;
2) Now we need a form to allow our visitors to submit their own sites. This script will also display all approved affiliates.
Call this script affiliates.php.
<?php
oB_start();
include ("config.php");
?>
<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" style="border-collapse:collapse" >
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td><strong>Affiliates</strong></td>
</tr>
<tr>
<td><table width="90%" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td>
<?php
// this script will display all the approved affiliates. the code above is just the start of a table to hold the data in.
$query="SELECT * from affiliates where approved = 1"; //gets all the approved affiliates..
$result=mysql_query($query) or die ("Could not execute query: $q.". mysql_error());
while($row=mysql_fetch_array($result)) //get all the data...
{
extract($row); // extract row values...this gets $url, $banner and $id
// now let's format how the avatars will be displayed
?>
<?php echo "<a href=\"$url\" target=\"_blank\"><img src=\"$banner\" width=\"88\" height=\"31\" border=\"0\">";?>
<?php
} // end of while statement..this is important or you will get an error. This echoes out an 88x31 image size, with the appropriate banner and URL attached. It opens in a new window.
?></td>
</tr>
</table>
</td>
</tr>
<tr>
<td><strong>Affiliate with My Site!</strong></td>
</tr>
<tr>
<td><br>
<br>
Affiliate today, and help out a new site, and bring some more traffic to your own site! </td>
</tr>
<tr>
<td>
<form action="affiliates.php" method="post" name="affiliate" id="affiliate">
<table width="60%" border="0" align="center" cellpadding="4" cellspacing="0">
<tr>
<td><strong>Website URL</strong></td>
<td><input name="url" type="text" id="url" size="50"></td>
</tr>
<tr>
<td><strong>Banner URL</strong></td>
<td><input name="banner" type="text" id="banner" size="50"></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="submit" type="submit" id="submit" value="Submit">
</div></td>
</tr>
</table>
<div align="center"><br><?php
if (isset($_POST['submit'])) {
//if the form was submitted...
$url = stripslashes(strip_tags(htmlspecialchars($_POST['url'], ENT_QUOTES)));
$banner = stripslashes(strip_tags(htmlspecialchars($_POST['banner'], ENT_QUOTES)));
//checks if any was left empty..
if(empty($url) || empty($banner))
{
echo "Error! Please go back and fill in all the required fields!";
die; // stops the script from executing the rest of the code
}
//else its all ok, so:
$query = "INSERT INTO affiliates (url, banner)
VALUES ('$url','$banner')";
mysql_query($query);
echo "Thanks for applying! Your site will be reviewed before being accepted.";
}
?>
<br>
</div>
</form></td>
</tr>
</table></td>
</tr>
</table>
3)Now we need an admin control panel to sort out all our tutorials.
Save this as manageaffiliates.php
<?php oB_start();
include("config.php");?>
<?php echo("
<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"4\" bordercolor=\"#FFFFFF\">
<tr>
<td height=\"30\" bgcolor=\"#E4E4E4\"><strong>Manage Affiliates </strong></td>
</tr>
<tr>
<td><p>The following affiliates are awaiting approval:</p>
<p> ");?>
<?php //this gets all affiliates, and counts how many there are awaiting approval. If there are none, the user is told.
$result = mysql_query("SELECT * FROM affiliates where approved =0");
$num_rows = mysql_num_rows($result);
if ($num_rows == "0") {
echo "<b>There are no affiliates awaiting approval!</b></br>
";
} else { //there are some...
while($r=mysql_fetch_array($result))
{
$id=$r["id"];
$url=$r["url"];
$banner=$r["banner"];
//pulls out the data...
echo("
<table width=300 cellpadding=\"2\" cellspacing=\"5\" border=\"1\" bordercolor=\"#FFFFFF\">
<tr><td width=72 rowspan=\"2\" align=\"center\">
<div align=\"center\"><img src=\"$banner\">
</div></td>
<td width=\"194\" rowspan=\"2\">
<b>Site: </b><a href=\"$url\" target=\"_blank\">$url<br>
</td> //Creates a nicely formatted table for each affilate, complete with URL and banner.
<td width=\"34\"><a href=\"manageaffiliates.php?approve=yes&id=$id\">Approve this affiliate!</a></td> //creates a link allowing the affiliate to be approved.
</tr>
<tr>
<td><a href=\"manageaffiliates.php?delete=yes&id=$id\">Delete this Affiliate!</a></td> //no, we don't want this affiliate thank you very much!
</tr>
</table><p>");//close table..
} //end while.
}
//now we work out what the user chose:
if($_GET["approve"]=="yes") //we want to approve...
{
$id = $_GET['id']; //gets id of banner
$sql = "UPDATE affiliates SET approved='1' WHERE id=$id";
//runs a query to change that banners status to 1..so it can now be shown on affiliates.php
$result = mysql_query($sql);
echo "Approved."; //tells the user it worked
}
else if($_GET["delete"]=="yes") //else if delete was chosen...
{
$id = $_GET['id']; //gets id
$sql2 = "DELETE FROM affiliates WHERE id=$id"; //removes affiliate from database
$result2 = mysql_query($sql2);
echo "Deleted."; //tells user.
}
?><br>
</td>
</tr>
</table>
Thats all of it, It should working fine.