adv banner
Web Programming  Home Web Programming PHP Downloads CMS and Clicks Counter
rss

Downloads CMS and Clicks Counter

Author: GreyCobra.com More by this author


Downloads CMS and Clicks Counter1. There are 3 documents we will be creating: downloads.php, downloaditem.php, additem.php. You will need access to a mySQL Database with phpmyadmin, as well as a host with php installed onto it.

2. First lets set up the table in phpmyadmin. go to your database, and run the following SQL Command:

CREATE TABLE 'downloads' (
'id' int(11) NOT NULL auto_increment,
'title' varchar(255) NOT NULL default '',
'url' varchar(255) NOT NULL default '',
'count' smallint(11) NOT NULL default '0',
PRIMARY KEY ('id')
) TYPE=MyISAM PACK_KEYS=0 AUTO_INCREMENT=1 ;

3. This query will create a new table called downloads. Let me explain each column in more detail:

id - Each download you add will have its own unique id. The id increments by 1 for each download.
title - this will be the title of your download, for example 'My Download!'
url - this is the link to your actual download, for example 'http://www.greycobra.com/images/downloads/d/GreyCobra%20V4med.jpg'
count - this will keep track of how many people click your link

4. Hopefully you have a basic understanding of what you just queried now. Time to make downloads.php. downloads.php is going to query (or retrieve data from) the data base, and then display it in a way we can understand. Open up your favorite text editor (Context works great, and is completely free!). Type the following into it:

<?php
include("dbconnect.php"); //If you have not set up a file that contains your password data, type the following
$username = "username";
$password = "password";
$host = "your.host.com";
$database = "databasename";

mysql_connect($host,$username,$password) or die("Error connecting to Database!
" . mysql_error());

mysql_select_db($database) or die("Cannot select database!
" . mysql_error());

$getdownloads = mysql_query("SELECT * FROM downloads ORDER BY id DESC"); //Retrieves all of the Data from the downloads table in your database
while($row = mysql_fetch_assoc($getdownloads))
{
print(" //anything in here will be displayed
$row[title] - Downloaded $row[count] Times //Makes a link to the specific download item by its id, and then shows its tiles, and the number of times its been downloaded
");
}

?>

5. Now, save this as downloads.php, and create a new document. Write the following in it:

<?php
include("dbconnect.php");//again, if you don't have a file for this, make sure you use the code I supplied in part one in light green in place of this line

if ($id) {//if an id has been queried then...

$go = MYSQL_QUERY("SELECT * FROM v4downloads WHERE id = $id");//define go as a query


if(!$go) { die("Error!"); }//if there is a problem, slit wrists
$home = MYSQL_FETCH_ARRAY($go);//define home


MYSQL_QUERY("UPDATE v4downloads SET count = count + 1 WHERE ID = $home[id]");//update table downloads, by adding 1 to the count of the id specified
MYSQL_FREE_RESULT($go);


header("Location: $home[url]");//redirect to the url specified for the specific id
}
?>

6. This is the file that your downloads.php links to for each link. downloads.php will display all of the links, you DO NOT have to post the code multiple times. When a user clicks on a link from downloads.php, it will direct them to downloadfile.php?&id=$row[id]. What does that mean. Heres and example:

You add a download. It automatically obtains the id of one if it is the first download, 2 if it is the second download, 3 if its the third, etc. When you go to downloads.php, it is going to display all of your downloads as long as there are downloads to display. For download id 1, your link will be downloadfile.php?&id=1. You DO NOT have to type any of that though, downloads.php and downloadfile.php do all of that for you. When you click on this link from downloads.php, it will bring you to downloadfile.php, and replace all of the $id variables with the download id (for example id 1). Then, it will find the $url in that row (for example) and redirect you to it while adding 1 to the count!

7. So what good is this without a way of adding a download? That's what additem.php does! You would be smart to put this file in a password protected directory! Open up another document and type the following:

<?php
include("dbconnect.php");

//hopefully you have created a dbconnect file by now, if not, use the same code as the others

<?php
include("dbconnect.php");//hopefully you have created a dbconnect file by now, if not, use the same code as the others
if(!empty($title)) {//if title is not empty, than do this...
$title = addslashes($title);
$url = addslashes($url);

$sql = "INSERT INTO downloads (id, title, url) VALUES ('NULL', '$title','$url')";//insert the title and url into the table downloads
$query = mysql_query($sql) or die("ERROR<br>" . mysql_error());
echo "Database Updated.";
} else {//if title is empty, than do this instead...
?>
<form name="news" method="post" action="<?php echo $PHP_SELF; ?>">
<p>Please fill out all of the following fields:</p>
<table width="100%" border="0" cellpadding="0" cellspacing="0">

<tr>
<td width="117"><font size="1">Title*: </font> </td>
<td width="577">
<font size="1">
<input type="text" name="title" size="50">
</font>
</td>
</tr>
<tr>


<tr>
<td width="117"><font size="1">URL*:</font></td>
<td width="577">
<font size="1">
<input type="text" name="url" size="50">
</font>
</td>
</tr>


</table>
<p>

<input type="submit" name="Submit" value="Submit"></font>
</p>
</form> <?php
}
?>

And that's it! Good luck!



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Downloads CMS and Clicks Counter"