Help - Search - Members - Calendar
Full Version: [PHP] - [Simple download counter] - [Gio]
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Gio
Ok this tutorial will simply show you how to display the number of times a certain file has been downloaded. It can also be altered to show number of times it was clicked.

The SQL

CODE
CREATE TABLE counter (
id int(5) DEFAULT '0' NOT NULL auto_increment,
FileName varchar(50) NOT NULL,
FileURL varchar(255) NOT NULL,
Count int(5) NOT NULL,
PRIMARY KEY (id)
);


Basically the sql table has fields for the id, which is unique for each file, the file name, the file url, and the count.

The viewing of the file.

View.php

CODE
<?php

include "connect.php";

$x ="SELECT * from counter order by FileName";
$result = mysql_query($x) or die
("Could not execute query : $x." . mysql_error());

echo "<p align=center><b>File Download</b></p>";
echo "<ol>";

while ($row=mysql_fetch_array($result))
{
$id = $row["id"];
$FileName = $row["FileName"];
$FileURL = $row["FileURL"];
$Count =$row["Count"];


echo "<li><a href=download.php?id=$id>$FileName</a></li>";
}
?>


First you connect then you select the downloads and order them by the filename, and if sql will not connect the script dies immediately. Then you echo File Download fetch the resaults of your previous query and order them out to look pretty, then link them!

Now for the download counter, and of course the download!

Download.php


CODE
<?php

// Database connection stuff here

$x ="SELECT * from counter where id='$id'";
$result = mysql_query($x) or die
("Could not execute query : $x." . mysql_error());

while ($row = mysql_fetch_array($result))
{
$id = $row["id"];
$FileName = $row["FileName"];
$FileURL = $row["FileURL"];
$Count = $row["Count"];

header('Location: '.$FileURL);

$x ="update counter set Count=Count+1 where id='$id'";
$result = mysql_query($x) or die
("Could not execute query : $x." . mysql_error());

}

?>


Ok I shall explain this part as well. First you query the database and get the id of the file selected. Then it finds it or the script automatically dies. Define the id filename fileurl and the times downloaded. It redirects the user to the file, and adds 1 more number to the download stats.
Jay
nice tut, nice to see p2l can write them as well as provide links to them victory.gif
Gio
Yah I am still not sure why Faken never premoted me to staff, but I am just trying to get the p2l tut count up so were not just an indexing service.
Anarchy
very useful... victory.gif
Gio
Thanks dude, I was wondering if anyone actually read them.
SERB-4-LIFE
i do i read every single one in the forum and heaps from the site victory.gif
Andy
^^ nice... alot of that confuses me as well as it has been explained.

PHP is not my thing, feels good when you set something up though, HAHA. Nice tut, will definatley come in handy!

Thanks,
Andy smile.gif
SERB-4-LIFE
well its not my thing but it is when someone does all the code for me and tells me how to use it victory.gif but all i can write in php is <?php ?> thats it victory.gif

so thats whats so special about this site, it gives me the code and its easy to use
Gio
Hey I am always happy to provide. Just do not ask for everything, it is easier sometimes to try to do it yourself then ask for help with what ever your not getting. That way you dont seem greedy.
SERB-4-LIFE
i did as much as i could with no help now i need it victory.gif
Gio
Sorry but I do not think of

CODE
<?

?>



As real coding, I mean put some real effort into it. Things will not always be done for you for free. Sometimes you gotta give alittle to get alittle.
SERB-4-LIFE
yeh i know victory.gif is there a site where it learns u step by step php ??
Jay
i'd love to create such a site if i ever got time
Gio
That would take sooo long though, try W3 Schools. They teach you stuff step by step and even provide tests for you to take.
Anarchy
yeah, w3schools look great, i just started, and it looks like they provide pretty much everything.
Gio
Kind of, pretty much as basic as it gets.
SERB-4-LIFE
yeh but it takes a while to finish all of it
Gio
Anyways back on topic, SERB did you decide to use this one?
SERB-4-LIFE
yeh i did when u come to msn ull give me a leson on how to use this victory.gif i want to use it but its 2 complicated so ull help me out victory.gif
Gio
I will do, just post any problems with it here so they can be fixed immediately.
SERB-4-LIFE
ok Gio here is the problem i added the file through mysql that was easy now i get an error so have a look at my connect file if everything is good with it

CODE
<?php
                          $username = "username";
                           $password = "password";
                           $host =  "host";
                           $database = "database";

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

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

i canged username, pass,host,databasename for protection victory.gif but i have the corect username and pass i can connect to the database and i get this error

CODE
Could not execute query : SELECT * from counter where id='1'.No Database Selected
Jamie Huskisson
remove double spacing between the equals sign and value of $host ?

it could be that you've forgotten to end an existing database connection before you've made that one
Gio
QUOTE(SERB-4-LIFE @ Sep 27 2004, 04:31 AM)
ok Gio here is the problem i added the file through mysql that was easy now i get an error so have a look at my connect file if everything is good with it

CODE
<?php
                          $username = "username";
                           $password = "password";
                           $host =  "host";
                           $database = "database";

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

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

i canged username, pass,host,databasename for protection victory.gif but i have the corect username and pass i can connect to the database and i get this error

CODE
Could not execute query : SELECT * from counter where id='1'.No Database Selected

CODE
<?php
$username = "username";
$password = "password";
$host =  "localhost";
$database = "database";

$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());
?>


I would do it like that
InFnit
Um.....is that a copy and paste code? If so where does the connect.php come from?

Can this be explained a bit more since the only understanding on php i have is <? include() ?> but Im trying to learn.

So can someone please explain this a bit more? Maybe Gio you can add me smile.gif

lwendzich@gmail.com = MSN
infnit123 = AIM
JaK3rIUS
great man. lookin' for something like this. thanks
adam123
QUOTE(Gio @ Sep 27 2004, 10:07 PM)
CODE
<?php
$username = "username";
$password = "password";
$host =  "localhost";
$database = "database";

$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());
?>


I would do it like that



I would do it like this:
CODE
if ($dbc = @mysql_connect ('localhost', 'username', 'password'))
{
if (!@mysql_select_db ('dbname'))
{
die ('<p>Could not select the database because: <b>' . mysql_error() . '</b>.</p>');
}
}
else
{
die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b>.</p>');
}
adrian
This is a great script and works well. I added two extra fields, description and image. And I changed the echo so that it should also echo the descriptiona and image.

Note, I have changed table and column names for my use but they still worked fine. I also added
CODE
$dld_description =$row["dld_description"];
$dld_image =$row["dld_image"];

to
CODE
while ($row=mysql_fetch_array($result))
{
$id = $row["id"];
$dld_file_name = $row["dld_file_name"];
$dld_file_url = $row["dld_file_url"];
$dld_count =$row["dld_count"];

in view.php. The file name shows up, the download link works and it also shows the number of downloads, but it wont display the description or image, this is my echo code:
CODE
echo "<table width=100%  border=0 cellspacing=0 cellpadding=0>
 <tr>
   <td width=155><div align=center class=body>Download ID: $id</div></td>
   <td class=bold>$dld_file_name</td>
 </tr>
 <tr>
   <td><div align=center><img src=$dld_image></div></td>
   <td class=body>$dld_description</td>
 </tr>
 <tr>
   <td><div align=center class=body><a href=includes/download.php?id=$id>Click Here To Download</a></div></td>
   <td><div align=right class=body>Number of Downloads: $dld_count</div></td>
 </tr>
</table><Br>";

How can I get it to show the description and image. Thanks.

-Adrian
adrian
*bump* sorry, i had to, I still have this problem you know!
Ruben K
way to necropost
bondy
Gio

I have somethng similar, however I want a log to record when a link has been used.

At the moment the user is given a list of links he can download. Selecting one sends him to a download.php page which displays the one link to download.

On this page the user and code is logged, however the user may decide not to select the link to download therefore the log is false.

please help.
madcom
my header function gives an error... what can i do..
please help me
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.