PDA

View Full Version : Tutorial: Basic Affiliate System



мϊкэ
08-08-2005, 06:50 PM
Ross has requested this tutorial to know how the affiliate system works on my site! ;)

Right, this tutorial will explain how to:
Have a page which will show random affiliates a number of them of your choice
Have a page which will count how many clicks each link recieves
Have a page which will show all of the affiliates and how many clicks they recieve
Have a page which will allow you to add affiliates to the current list ;

Right first of all you will need to create the tables required in the mysql db, in phpmyadmin run this query in your db:


CREATE TABLE `affiliates` (
`id` int(11) NOT NULL auto_increment,
`url` varchar(255) NOT NULL default '',
`imgurl` varchar(255) NOT NULL default '',
`total` int(11) NOT NULL default '0',
PRIMARY KEY (`id`),
FULLTEXT KEY `url` (`url`)
) ;

right now that you've got the table sorted you'll need to connect to the database! create a file called affconnect.php, add this code to the page


<?php
$username = ""; //database username
$password = ""; //database password
$host = "localhost"; // database host (Usually localhost)
$database = ""; // database name
$connect = mysql_connect($host,$username,$password) or die("Error connecting to Database! " . mysql_error());
mysql_select_db($database , $connect) or die("Cannot select database! " . mysql_error()); //attempts to connect to database
?>

fill in your mysql details and save :D, right now to show random affiliates, create a page called affiliates.php and add this code to it


<?php
include ("affconnect.php"); // Connects to the DB

$random = mysql_query("select * from affiliates ORDER BY RAND() LIMIT 0, 10"); // Queries the database

while($r=mysql_fetch_array($random)){ //while loop

extract($r);

echo "<center><a href='count.php?id=$id' target='_blank'>";
echo "<img border='0' height='31' width='88' vspace='2' src='$imgurl'></a></center>";
}
?>


now configuring it, to change the amount of random affiliates to show change


$random = mysql_query("select * from affiliates ORDER BY RAND() LIMIT 0, YOUR NUMBER"); // Queries the database

the part which says YOUR NUMBER to the amount you would like to show, if you would like to change how the affiliates are layed out you must edit this:


echo "<center><a href='count.php?id=$id' target='_blank'>";
echo "<img border='0' height='31' width='88' vspace='2' src='$imgurl'></a></center>";

but you must not change the hyperlink location unless count.php is in another directory, and $imgurl must stay the same!
Now showing all of the affiliates together, create a page called allaff.php and add this code to it:


<?php
echo "<center><table width='100%' cellspacing='2' cellpadding='0'><tr>";
include ("affconnect.php");
$result = mysql_query("SELECT * FROM affiliates ORDER BY total DESC");

while ($row = mysql_fetch_object($result)) { // while loop While you are connected to the database do this...

echo "<td align='center' style='font-face: verdana; font-size: 9px;'><a href='count.php?id=".$row->id."' target='_blank'><img src='".$row->imgurl."' height='31' width='88' border='0' vspace='2'></a><br>Hits Out: ".$row->total."</td>";
}
echo "</tr></table></center>";

?>


this will order all of your affiliates by how many clicks it has recieved like this

Most Click -------- Least clicks

you can also edit the layout of how you want to display the affiliates by editing the echo parts. Now counting the clicks create a file called count.php and add this code to it:


<?php
include ("affconnect.php");
$id = $_GET['id']; //Gets the id from the browser, ie, count.php?id=2
if (!is_numeric($id)) //if the id is NOT a number exits the script
{
exit;
}
mysql_query ("UPDATE affiliates SET total = total + 1 WHERE id = '$id'"); //makes a database query and updates the field named total with the previous total +1(for the click)
$result = mysql_query("SELECT * FROM affiliates WHERE id = '$id'"); //makes another query and selects all from the table affiliates where field id equals the variable id(which we defined as the GET function)
$row = mysql_fetch_object ($result);
header("Location: " . $row->url); //makes a redirect to the specified URL of the link found in the database.
?>

This code for count.php is basically the same as the code for count.php in my download cms tutorial it actually came from here first though and i applied it to the download cms. Now create a page called addaff.php and add this code to it:


<?php
include('affconnect.php');//include the file that connects to the database
if(!empty($imgurl)) {//if the title is not empty, than do this function
$url = addslashes($url);
$imgurl = addslashes($imgurl);
$total = addslashes($total);

$sql = "INSERT INTO affiliates (id, url, imgurl, total) VALUES ('NULL', '$url','$imgurl','$total')";//Insert all of your information into the mynews table in your database
$query = mysql_query($sql) or die('Cannot query the database.<br>' . mysql_error());
echo "<center>Affiliate Added.</center>";
} else {
?>
<form name='news' method='post' action='addaff.php'>
<p>Please fill out all of the following fields:</p>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='117'>Site URL*: </td>
<td width='577'>

<input type='text' name='url' size='50'>

</td>
</tr>
<tr>
<td width='117'>Link URL (88x31 Button)*:</td>
<td width='577'>
<input name='imgurl' type='text' size='50'>
</td>
</tr>
<tr>
<td width='117'>Total Hits*:</td>
<td width='577'>
<input name='total' value='0' onFocus='this.blur()' type='text' size='50'>
</td>
</tr>
</table><br>
<input type='submit' name='Submit' value='Submit'>
</form>
<?php
}
?>

this code is just a basic form script which will insert the data you add to the form into your mysql database, the total hits input has been locked so 0 hits will be what the affiliate starts off with.

These are the codes i am currently using on xenigma.co.uk except i have password protected the add page, i will write a tutorial on doing this soon!, any problems with these codes PM me!

Coming soon...
Editing Affiliates
Deleting Affiliates
Password protecting the add page

For more tutorials including this one check out xenigma.co.uk, this tutorial will also be stickied in my sub-forum :D

Anderman
08-08-2005, 07:09 PM
Damn, Great tut man :D

мϊкэ
08-08-2005, 07:17 PM
:O before that post you just did you had 1337 posts!! rofl

iRoss
08-08-2005, 09:01 PM
Another great tut Mike!

Want to hide these adverts? Register an account for free!