I am having problems trying to get my system to count the number of times a files has been downloaded. The website has download buttons linking to the files which are stored on my database, and I would like to be able to count the amount of downloads per file to display as a statistic, ideally once they click the link to download, the column in the files table should be incremented,but i cant do that. i'm beginner in php. somebody can help me?this is my code to display the file.
<h3 class="box-title">Senarai Borang Cuti</h3>
<p> </p>
</div><!-- /.box-header -->
<div class="box-body">
<!-- Advanced Tables -->
<div class="table-responsive">
<?php
$raw_results = mysql_query("SELECT * FROM document WHERE doc_jenisfail= 'Cuti';");
?>
<table width="98%" class="table table-striped" id="dataTables-example">
<thead>
<table width="600" border="1">
<tr>
<th width="190" bgcolor="#756E37"class="sw" style="text-align: centre" scope="col">Tajuk Borang</th>
<th width="30" bgcolor="#756E37" class="sw" style="text-align: centre" scope="col">Tarikh Upload</th>
<th width="30" bgcolor="#756E37" class="sw" style="text-align: centre" scope="col">Memuat Turun</th>
</tr>
</thead>
<tbody>
<?php
while($results = mysql_fetch_array($raw_results)) { ?>
<tr>
<td align="left"><?php echo $results['nama_file'];?></td>
<td align="center"><?php echo $results['tanggal_upload'];?></td>
<td align="center"><a href="admin/<?php echo $results['file'] ?>" target= "_blank"><img src="images/download.png?key=<?php $results['nama_file'] ?>" /></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</p></td>
You can add +1 to downloads in your database and then redirect to file.
In your template use link like download.php?id=1
, for downloading file with id 1.
Download.php Code:
<?php
//name of file: download.php
$id = $_GET['id'];
//Check if the get number is really a number
if (ctype_digit($id)) {
//Add +1 to downloads
mysql_query("update `document` set `downloads`=`downloads`+1 where id='$id'") or die(mysql_error());
//Redirect to downloads
$download_file = mysql_fetch_array(mysql_query("select * from document where id='$id'")) or mysql_error();
header("Location: http://example.com/pdfs/$download_file[name].pdf");
} else {
echo 'Wrong link';
}
?>