PHP Development Board php divider

User Options
Register--Login--Top 20 Posters--Search Topics


Forum Main>>Tutorials>>Button affiliate system in PHP/MYSQL

New Topic-Reply




Author
Post
Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged

PM ID and RPS ID: 1
PM [Chipmunk]

View Member Photo

Last replied to on Mon Dec 12, 2011 01:37:34
Edit Post|Quote
This tutorial will teach you how to build a simple button links system and keep track of the number of visitors you send to your affiliates.
This tutorial will require PHP AND MYSQL so please be sure to have those. This tutorial will be fairly simple, we will need the main file folder and an admin folder that will be password protected. So create an admin folder that is password protected. Most control panels have the ability to do this. If yo do not have a control panel go to http://www.javascriptkit.com/howto/htaccess3.shtml to get the .htaccess to password protect directories.
First we need to create the mySQL table for this script. We will call this table affiliates_table with the following fields:

affid - bigint, primary, auto-increment
url - varchar length 255
image - varchar length 255
hitsout - bigint

Now that we've created the database table we will start off with the files that need to be in the admin folder(the password protected folder).
We will need just 5 files here:


connect.php -- The mysql Connection file
addlink.php -- add a link button
modbutton.php -- lists all the current buttons to modify and delete
editbutton.php -- Edit a button link
deletebutton.php - Delete the button

For easy navigation, please put this at the top of all files in the admin folder:

Code:

<A href='addlink.php'>Add a link</a> - <A href='modbutton.php'>Edit/delete buttons</a>


These are just simple hyperlinks.

Now we'll move onto connect.php, the connection file:

Code:

<?php
$db = mysql_connect("localhost", "username", "password") or die("Could not connect.");
if(!$db)
die("no db");
if(!mysql_select_db("database_name",$db))
die("No database selected.");
?>


This is a simple connector file, put your mySQL username,password, and database name where indicated, we will want to include this file in at the top of all pages.

Now we will go to addlink.php

Code:

<?php
include "connect.php";
if(isset($_POST['submit']))
{
$url=$_POST['url'];
$button=$_POST['button'];
if(strlen($url)<1)
{
print "You did not enter a URL.";
}
else if(strlen($button)<1)
{
print "You did not enter a button.";
}
else
{
$insertbutton="INSERT into affiliates_table (url, image) values('$url','$button')";
mysql_query($insertbutton) or die(mysql_error());
print "Button added into system.";
}

}
else
{
print "<form action='addlink.php' method='post'>";
print "URL(include http://):<br>";
print "<input type='text' name='url' size='20'><br>";
print "Image(button URL):<br>";
print "<input type='text' name='button' size='20'><br>";
print "<input type='submit' name='submit' value='submit'></form>";

}
?>

As you can see, this code has two cases, a case if it detects if the submit button has been pushed as indicated by $_POST['submit'], and an else case where it just prints a form for you to input a button. The form has two fields, the url field and the button url field. Both these fields are required or when you submit the form, you will get an error message telling you that you either did not enter a URL or a button. After you press submit and the form detects both a url and a button url entry, it then inserts the data you entered with a mySQL INSERT command. Note in the insert statement the stuff in the first parenthesis are the names of the fields in which you created in mySQL and the stuff in the 2nd parenthesis are the variable with the data that will be filled in. If the SQL insert is successful if will print "Button added to system", otherwise it will throw a error as defined by mysql_error() .

Now lets go to modbutton.php:

Code:

<?php
include "connect.php";
$getbuttons="SELECT * from affiliates_table order by hitsout desc";
$getbuttons2=mysql_query($getbuttons) or die("Could not get buttons");
print "<table border='1'>";
print "<tr><td>Button link</td><td>Hits out</td><td>Edit</td><td>Delete</td></tr>";
while($getbuttons3=mysql_fetch_array($getbuttons2))
{
print "<tr><td><A href='$getbuttons3[url]' target='_blank'><img src='$getbuttons3[image]' border='0'></a></td>";
print "<td>$getbuttons3[hitsout]</td><td><A href='edit.php?ID=$getbuttons3[affid]'>Edit</a></td>";
print "<td><A href='delete.php?ID=$getbuttons3[affid]'>Delete</a></td></tr>";
}
print "</table>";
?>


This code simply selects all the button links in the database and displays them by the number hits out you've given them, with the greatest number of hits out first. It loops through the query and prints out the button link, the number of hits out the link has, and two links to edit and delete the link.

Now we will go to the edit.php page.

Code:

<?php
include "connect.php";
if(isset($_POST['submit']))
{
$url=$_POST['url'];
$image=$_POST['image'];
$ID=$_POST['id'];
if(strlen($url)<1)
{
print "You did not enter a url.";
}
else if(strlen($image)<1)
{
print "You did not enter an image.";
}
else
{
$updatelink="Update affiliates_table set url='$url',image='$image' where affid='$ID'";
mysql_query($updatelink) or die(mysql_error());
print "Link updated.";
}

}
else
{
$ID=$_GET['ID']; //gets the id from URL
$getbutton="SELECT * from affiliates_table where affid='$ID'";
$getbutton2=mysql_query($getbutton) or die("Could not get button");
$getbutton3=mysql_fetch_array($getbutton2);
print "<form action='edit.php' method='post'>";
print "<input type='hidden' name='id' value='$ID'>";
print "URL:<br>";
print "<input type='text' name='url' value='$getbutton3[url]' size='20'><br>";
print "Image URL:<br>";
print "<input type='text' name='image' value='$getbutton3[image]' size='20'><br>";
print "<input type='submit' name='submit' value='submit'></form>";


}
?>


This looks similar to the addlink.php page because essentially it is the same thing, except you are using the ID passed in from the URL to grab the information for the specific button you want to edit and then using that information to pre-populate the fields in the form. Once the submit button is pushed, it checks to make sure if there is any data in url and image and if there is, it updates that button record with the new information using the UPDATE query from mySQL.

Now we will go to the last page in the admin folder, delete.php

Code:

<A href='addlink.php'>Add a link</a> - <A href='modbutton.php'>Edit/delete buttons</a>

<?php
include "connect.php";
if(isset($_POST['submit']))
{
$ID=$_POST['ID'];
$delbutton="Delete from affiliates_table where affid='$ID'";
mysql_query($delbutton) or die("Could not delete button");
print "Button deleted.";

}
else
{
$ID=$_GET['ID']; //gets the id from URL
print "<br>Are you sure you want to delete this button affiliate?<br>";
print "<form action='delete.php' method='post'>";
print "<input type='hidden' name='ID' value='$ID'>";
print "<input type='submit' name='submit' value='submit'></form>";

}
?>


This is a simple file, it uses the ID passed in and puts that ID in a hidden field, when you hit the submit button, it deletes the button entry with that ID.
Now that the administration stuff is done, we will go to the frontend and write the simple code for displaying the buttons.
So in your main folder we will need two files:

aff.php -- The page that displays the buttons
out.php -- The page that transfers counts how many times a button has been clicks and redirects to the actual site the button is pointing to.

so in aff.php:

Code:

<?php
include "admin/connect.php";
$counter=1;
$getbuttons="SELECT * from affiliates_table order by hitsout desc";
$getbuttons2=mysql_query($getbuttons) or die("Could not get buttons");
while($getbuttons3=mysql_fetch_array($getbuttons2))
{
if($counter%4==0)
{
print "<A href='out.php?ID=$getbuttons3[affid]'><img src='$getbuttons3[image]' border='0'></a><br>";
}
else
{
print "<A href='out.php?ID=$getbuttons3[affid]'><img src='$getbuttons3[image]' border='0'></a>&nbsp;&nbsp;";
}
$counter++;
}
?>


So this code select all the buttons in the database table and then sets a counter to see how many you've looped through, in this code example if there already been 4 buttons in a row, it wil start a new line as indicated by <br>. Now we need out.php to actually count the number of hits out and redirect to the actual URL.

Code:

<?php
include "admin/connect.php";
$ID=$_GET['ID'];
$getbuttons="SELECT url from affiliates_table where affid='$ID'";
$getbuttons2=mysql_query($getbuttons) or die("Could not get buttons");
$getbuttons3=mysql_fetch_array($getbuttons2);
$updatehits="Update affiliates_table set hitsout=hitsout+1 where affid='$ID'";
mysql_query($updatehits) or die("Could not update hits");
//time for a meta-refresh direct
print "<META HTTP-EQUIV = 'Refresh' Content = '1; URL =$getbuttons3[url]'> ";
?>


What this does is it gets the ID from the URL, then it selects just the URL of the record with that ID because we need to URL to redirect to the actual site. Then it updates the number of hits for the record with that ID and increments it by 1. Finally it does a meta redirect to the real url.

Thats it for your button system!

-----------------------------
Chipmunk,
Supreme Administrator

Page:



Powered by © Chipmunk Board

Flash games Ninja games-Web Design New York