Jump to content


* * * - - 1 votes

[PHP] - [Simple download counter] - [Gio]


  • Please log in to reply
30 replies to this topic

#1 _*Gio_*

_*Gio_*
  • Guests

Posted 05 September 2004 - 03:26 PM

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

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

<?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


<?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.

#2 _*Jay_*

_*Jay_*
  • Guests

Posted 05 September 2004 - 03:34 PM

nice tut, nice to see p2l can write them as well as provide links to them :P

#3 _*Gio_*

_*Gio_*
  • Guests

Posted 05 September 2004 - 03:35 PM

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.

#4 _*Anarchy_*

_*Anarchy_*
  • Guests

Posted 05 September 2004 - 04:40 PM

very useful... :D

#5 _*Gio_*

_*Gio_*
  • Guests

Posted 05 September 2004 - 09:32 PM

Thanks dude, I was wondering if anyone actually read them.

#6 _*SERB-4-LIFE_*

_*SERB-4-LIFE_*
  • Guests

Posted 07 September 2004 - 01:30 AM

i do i read every single one in the forum and heaps from the site :D

#7 _*Andy_*

_*Andy_*
  • Guests

Posted 07 September 2004 - 02:29 PM

^^ 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 :D

#8 _*SERB-4-LIFE_*

_*SERB-4-LIFE_*
  • Guests

Posted 07 September 2004 - 05:21 PM

well its not my thing but it is when someone does all the code for me and tells me how to use it :D but all i can write in php is <?php ?> thats it :D

so thats whats so special about this site, it gives me the code and its easy to use

#9 _*Gio_*

_*Gio_*
  • Guests

Posted 08 September 2004 - 02:41 PM

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.

#10 _*SERB-4-LIFE_*

_*SERB-4-LIFE_*
  • Guests

Posted 08 September 2004 - 05:16 PM

i did as much as i could with no help now i need it :P

#11 _*Gio_*

_*Gio_*
  • Guests

Posted 08 September 2004 - 06:05 PM

Sorry but I do not think of

<?

?>


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.

#12 _*SERB-4-LIFE_*

_*SERB-4-LIFE_*
  • Guests

Posted 08 September 2004 - 10:49 PM

yeh i know :rolleyes: is there a site where it learns u step by step php ??

#13 _*Jay_*

_*Jay_*
  • Guests

Posted 09 September 2004 - 11:57 AM

i'd love to create such a site if i ever got time

#14 _*Gio_*

_*Gio_*
  • Guests

Posted 09 September 2004 - 02:43 PM

That would take sooo long though, try W3 Schools. They teach you stuff step by step and even provide tests for you to take.

#15 _*Anarchy_*

_*Anarchy_*
  • Guests

Posted 12 September 2004 - 03:48 PM

yeah, w3schools look great, i just started, and it looks like they provide pretty much everything.

#16 _*Gio_*

_*Gio_*
  • Guests

Posted 12 September 2004 - 05:05 PM

Kind of, pretty much as basic as it gets.

#17 _*SERB-4-LIFE_*

_*SERB-4-LIFE_*
  • Guests

Posted 13 September 2004 - 04:34 AM

yeh but it takes a while to finish all of it

#18 _*Gio_*

_*Gio_*
  • Guests

Posted 16 September 2004 - 12:46 PM

Anyways back on topic, SERB did you decide to use this one?

#19 _*SERB-4-LIFE_*

_*SERB-4-LIFE_*
  • Guests

Posted 20 September 2004 - 02:00 AM

yeh i did when u come to msn ull give me a leson on how to use this :rolleyes: i want to use it but its 2 complicated so ull help me out :blink:

#20 _*Gio_*

_*Gio_*
  • Guests

Posted 20 September 2004 - 12:46 PM

I will do, just post any problems with it here so they can be fixed immediately.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users