Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting downloads using <a href=# onClick="phpfunction()"> 1

Status
Not open for further replies.

m1bzm

ISP
Mar 6, 2001
31
GB
Hi all,

I want to be able to count downloads of files. Ideally Id love to be able to type <a href=# onClick=&quot;counter()&quot; where counter is a php function, but obviously i cant.

Can anyone think of a workaround for this alogrithm ( I know the code is wrong its the problem im trying to solve not the syntax)

--snip---
<a href=# onClick=&quot;counter()&quot;>CLICK HERE TO DOWNLOAD FILE </a>

<?php
function counter()
{
tempcounter=select counter from table
tempcounter++
update table set counter=tempcounter
}?>

I dont want to increment the counter until the link to the file is actually clicked.
How do other people count file downloads using PHP / MYSQL?

Thanks
Darren
 
Well, a browser would try to <a href=# onClick=&quot;counter()&quot;>CLICK HERE TO DOWNLOAD FILE </a> and it would look for a JavaScript function called counter(), not a PHP one.

What you could do though is, on the download page, pass a variable that is the name or id of the file to be downloaded, then have a field in a mysql database in a downloads table named num_downloads and increment that one whenever someone visits a page.

Heres what I mean:

[tt]
Pretend this is index.php....
<?php
// execute a query to find the id of all the things to download, then make a link like this:
print &quot;<a href=\&quot;download.php?id=$id\&quot;>Click here to download a $prod_name</a><br>\n&quot;;
// where $id and $prod_name are values you pull from the database
?>

Then here is download.php

<?php
// make a query to update the number of downloads in the databae by one.
$query = &quot;UPDATE downloads SET num_downloads=num_downloads+1 WHERE id=$id&quot;;

// execute that and you should be on your way.

?>

[/tt]

Hope this helps.

-Vic
vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Hi Vic

Thanks for your help. I think your on to something...
using your example...

in download.php, lets say ive successfully counted the request, i will need to start the download automatically in order for the whole thing to be done from 1 click. any ideas? That means that download.php will not only count the hits on the page but it also forces a file download. I think if I get that working thats about as close as Im going to get what I need.

Thanks for your m8.
Darren
 
Ok, can do.

After you update the number of hits, send a http header (with the header() function) to the location of the file.

i.e.,

[tt]
//download.php
//update database
header(&quot;Location:
[/tt]

And that will start the file download.

Hope this helps,

-Vic

vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Thats what I want to hear - Thanks Vic, thats a great help... I think you've solved it!

Thank you...

Darren
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top