Forum Moderators: coopster

Message Too Old, No Replies

Simple Download Counter

Help with logic?

         

ksdfla

9:55 pm on Feb 8, 2005 (gmt 0)

10+ Year Member



Hi,

I'm using a "snippet" of code for a download counter, which is this, and it's on my cat_download.php page:

<script language="php">
include "vsadmin/db_conn_open.php";
</script>

<!-- begin download counter -->

<?php
if($counted==1)
{
$result = mysql_query("SELECT counter FROM CatCount") or die ("Error in query: $query. " . mysql_error());
$data = mysql_fetch_array($result);
$count = $data[0];
}
else
{
$result = myslq_query("SELECT counter FROM CatCount") or die ("Error in query: $query. " . mysql_error());
$data = mysql_fetch_array($result);
$count = $data[0];
$count = $count+1;
mysql_query("UPDATE CatCount SET counter='$count'") or die ("Error in query: $query. " . mysql_error());
header("Location: /images/offcatalog.pdf");
}
?>
<!-- end counter script -->

I've tried the following code on the download link on my catalogtest.php page:

<p><strong><a href="cat_download.php?counted="">Download PDF Catalog</a></strong></p>

and that brings up the pdf but doesn't log any value in the database. Can anyone help me, please?

Thanks...

jatar_k

7:34 pm on Feb 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> href="cat_download.php?counted=""

when I look at that line there are 2 sets of double quotes at the end which means the value of href is equal to

cat_download.php?counted=

I am not sure if that is what it is supposed to be or not.

also is it only counting for a single file? I would think you should have a where clause in your update statement.

Are you getting any other errors? Have you used any other tests or echos to see exactly where it is having a problem?

NancyJ

10:20 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



I have a very simple downlaod script that I wrote, basically the referring page sends information about which file someone is trying to download, then finds how many times it has been downloaded and increments it.

<?
include 'db.php';
$file = $HTTP_GET_VARS['file'];
$downloads = $HTTP_GET_VARS['download'];

$sql = mysql_query("SELECT Downloads FROM Files WHERE File = '$file'");
$downloads = mysql_fetch_array($sql);
$counts = $downloads[0];
$counts=$counts+1;
mysql_query("UPDATE Files SET Downloads = $counts WHERE File = '$file'");
header("Location: $download");?>

Though in your case since its alway the same file being downloaded I would simplify it so that every time the page loaded it incremented a download, would the page ever be loaded with a value of counted=1?
It seems to me that the page should only load if someone is trying to download and therefore the 'if counted==1' is unecessary

NancyJ

10:23 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



It also occurs to me that at no point do you $_GET the value of Counted....