Affiliate System


1)First we need to create the table in our mysql database to allow us to store our affiliates information.

PHP Code
  1. SQL query:
  2. CREATE TABLE `affiliates` (
  3. `id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
  4. `url` VARCHAR( 100 ) NOT NULL ,
  5. `banner` VARCHAR( 100 ) NOT NULL ,
  6. `approved` TINYINT( 1 ) DEFAULT '0' NOT NULL ,
  7. PRIMARY KEY ( `id` )
  8. ) 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 Code
  1. <?php
  2. oB_start();
  3. include ("config.php");
  4. ?>
  5. <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" style="border-collapse:collapse" >
  6. <tr>
  7. <td><table width="100%" border="0" cellspacing="0" cellpadding="4">
  8. <tr>
  9. <td><strong>Affiliates</strong></td>
  10. </tr>
  11. <tr>
  12. <td><table width="90%" border="0" align="center" cellpadding="5" cellspacing="0">
  13. <tr>
  14. <td>
  15.  
  16. <?php
  17. // this script will display all the approved affiliates. the code above is just the start of a table to hold the data in.
  18.  
  19. $query="SELECT * from affiliates where approved = 1"; //gets all the approved affiliates..
  20. $result=mysql_query($query) or die ("Could not execute query: $q.". mysql_error());
  21.  
  22. while($row=mysql_fetch_array($result)) //get all the data...
  23. {
  24. extract($row); // extract row values...this gets $url, $banner and $id
  25.  
  26. // now let's format how the avatars will be displayed
  27. ?>
  28.  
  29. <?php echo "<a href=\"$url\" target=\"_blank\"><img src=\"$banner\" width=\"88\" height=\"31\" border=\"0\">";?>
  30. <?php
  31. } // 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.
  32. ?></td>
  33. </tr>
  34. </table>
  35. </td>
  36. </tr>
  37. <tr>
  38. <td><strong>Affiliate with My Site!</strong></td>
  39. </tr>
  40. <tr>
  41. <td><br>
  42. <br>
  43. Affiliate today, and help out a new site, and bring some more traffic to your own site! </td>
  44. </tr>
  45. <tr>
  46. <td>
  47.  
  48. <form action="affiliates.php" method="post" name="affiliate" id="affiliate">
  49. <table width="60%" border="0" align="center" cellpadding="4" cellspacing="0">
  50. <tr>
  51. <td><strong>Website URL</strong></td>
  52. <td><input name="url" type="text" id="url" size="50"></td>
  53. </tr>
  54. <tr>
  55. <td><strong>Banner URL</strong></td>
  56. <td><input name="banner" type="text" id="banner" size="50"></td>
  57. </tr>
  58. <tr>
  59. <td colspan="2"><div align="center">
  60. <input name="submit" type="submit" id="submit" value="Submit">
  61. </div></td>
  62. </tr>
  63. </table>
  64. <div align="center"><br><?php
  65.  
  66. if (isset($_POST['submit'])) {
  67. //if the form was submitted...
  68. $url = stripslashes(strip_tags(htmlspecialchars($_POST['url'], ENT_QUOTES)));
  69. $banner = stripslashes(strip_tags(htmlspecialchars($_POST['banner'], ENT_QUOTES)));
  70.  
  71. //checks if any was left empty..
  72. if(empty($url) || empty($banner))
  73. {
  74. echo "Error! Please go back and fill in all the required fields!";
  75. die; // stops the script from executing the rest of the code
  76. }
  77. //else its all ok, so:
  78. $query = "INSERT INTO affiliates (url, banner)
  79. VALUES ('$url','$banner')";
  80. mysql_query($query);
  81. echo "Thanks for applying! Your site will be reviewed before being accepted.";
  82. }
  83.  
  84. ?>
  85. <br>
  86. </div>
  87. </form></td>
  88. </tr>
  89. </table></td>
  90. </tr>
  91. </table>


3)Now we need an admin control panel to sort out all our tutorials.

Save this as manageaffiliates.php

PHP Code
  1. <?php oB_start();
  2. include("config.php");?>
  3.  
  4. <?php echo("
  5. <table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"4\" bordercolor=\"#FFFFFF\">
  6. <tr>
  7. <td height=\"30\" bgcolor=\"#E4E4E4\"><strong>Manage Affiliates </strong></td>
  8. </tr>
  9. <tr>
  10. <td><p>The following affiliates are awaiting approval:</p>
  11. <p> ");?>
  12.  
  13. <?php //this gets all affiliates, and counts how many there are awaiting approval. If there are none, the user is told.
  14.  
  15. $result = mysql_query("SELECT * FROM affiliates where approved =0");
  16. $num_rows = mysql_num_rows($result);
  17. if ($num_rows == "0") {
  18. echo "<b>There are no affiliates awaiting approval!</b></br>
  19. ";
  20. } else { //there are some...
  21.  
  22. while($r=mysql_fetch_array($result))
  23. {
  24.  
  25. $id=$r["id"];
  26. $url=$r["url"];
  27. $banner=$r["banner"];
  28. //pulls out the data...
  29. echo("
  30. <table width=300 cellpadding=\"2\" cellspacing=\"5\" border=\"1\" bordercolor=\"#FFFFFF\">
  31. <tr><td width=72 rowspan=\"2\" align=\"center\">
  32. <div align=\"center\"><img src=\"$banner\">
  33. </div></td>
  34. <td width=\"194\" rowspan=\"2\">
  35. <b>Site: </b><a href=\"$url\" target=\"_blank\">$url<br>
  36. </td> //Creates a nicely formatted table for each affilate, complete with URL and banner.
  37.  
  38. <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.
  39. </tr>
  40. <tr>
  41. <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!
  42. </tr>
  43. </table><p>");//close table..
  44. } //end while.
  45. }
  46. //now we work out what the user chose:
  47.  
  48. if($_GET["approve"]=="yes") //we want to approve...
  49. {
  50. $id = $_GET['id']; //gets id of banner
  51.  
  52. $sql = "UPDATE affiliates SET approved='1' WHERE id=$id";
  53. //runs a query to change that banners status to 1..so it can now be shown on affiliates.php
  54.  
  55. $result = mysql_query($sql);
  56. echo "Approved."; //tells the user it worked
  57. }
  58.  
  59. else if($_GET["delete"]=="yes") //else if delete was chosen...
  60. {
  61. $id = $_GET['id']; //gets id
  62.  
  63. $sql2 = "DELETE FROM affiliates WHERE id=$id"; //removes affiliate from database
  64.  
  65. $result2 = mysql_query($sql2);
  66. echo "Deleted."; //tells user.
  67. }
  68.  
  69. ?><br>
  70. </td>
  71. </tr>
  72. </table>


Thats all of it, It should working fine.
VicVance's Avatar
Author:
Views:
8,001
Rating:
There are currently no comments for this tutorial, login or register to leave one.