|
||||
| Register--Login--Top 20 Posters--Search Topics |
Forum Main>>Tutorials>>Button affiliate system in PHP/MYSQL | ||
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:
These are just simple hyperlinks. Now we'll move onto connect.php, the connection file:
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
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:
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.
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
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:
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.
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: |