SaLz
asked on
Two ways of doing things ?
Hi guys,
well I'm posting this in new thread as i said :)
As some of you might know, I'm making a download hit counter for two .exe files. In stats i want to see how many times each file has been downloaded each day ( in correct date format: day.month.year ), also i want a total number of downloads for each file, as you can see below, and a total downloads of both files.
File 1:
20.8.2005 - 20 downloads
19.8.2005 - 12 downloads
Total downloads of File 1: 32
File 2:
20.8.2005 - 30 downloads
19.8.2005 - 16 downloads
Total downloads of File 2: 46)
Total downloads of both files: 78
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
There are two ways of counting ( or more, feel free to post below :)
1st way: $insert_query="insert into DownloadHits values($fileID,'".$fileNam e."','".da te('Y-m-d' )."')";
// adding 1 record for each hit
------------
or 2nd way, something ive been working on:
// check if currentDate matches today's date, if not update it
if ( $currentDate <> date('Y-m-d') ) {
$currentDate = date('Y-m-d');
$result = mysql_query("UPDATE currentDate ...
// insert a new record with new date and add a hit to it
$Date = date('Y-m-d');
$result = mysql_query("INSERT INTO HitCounter SET
fileID = 1,
Date='$Date',
Hits=+1
");
// else update the hits where date is today and add 1 hit to it
} else {
$result = mysql_query("UPDATE HitCounter SET Hits=+1 WHERE Date='".date('Y-m-d')."')" ; }
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -------
Which method is better, or are there any better ones ?
And how can I do what I asked above ?
Thank you guys for your time :)
Sal
well I'm posting this in new thread as i said :)
As some of you might know, I'm making a download hit counter for two .exe files. In stats i want to see how many times each file has been downloaded each day ( in correct date format: day.month.year ), also i want a total number of downloads for each file, as you can see below, and a total downloads of both files.
File 1:
20.8.2005 - 20 downloads
19.8.2005 - 12 downloads
Total downloads of File 1: 32
File 2:
20.8.2005 - 30 downloads
19.8.2005 - 16 downloads
Total downloads of File 2: 46)
Total downloads of both files: 78
--------------------------
There are two ways of counting ( or more, feel free to post below :)
1st way: $insert_query="insert into DownloadHits values($fileID,'".$fileNam
// adding 1 record for each hit
------------
or 2nd way, something ive been working on:
// check if currentDate matches today's date, if not update it
if ( $currentDate <> date('Y-m-d') ) {
$currentDate = date('Y-m-d');
$result = mysql_query("UPDATE currentDate ...
// insert a new record with new date and add a hit to it
$Date = date('Y-m-d');
$result = mysql_query("INSERT INTO HitCounter SET
fileID = 1,
Date='$Date',
Hits=+1
");
// else update the hits where date is today and add 1 hit to it
} else {
$result = mysql_query("UPDATE HitCounter SET Hits=+1 WHERE Date='".date('Y-m-d')."')"
--------------------------
Which method is better, or are there any better ones ?
And how can I do what I asked above ?
Thank you guys for your time :)
Sal
ASKER
hi str_kani, yeah that's exactly why i made the logic plan for it, to keep the record count small but would i actually make it work. Can you show me how would i code it please ? should there be a seperate table for current date ?
anyhow can you show me how to do it correctly please :)
anyhow can you show me how to do it correctly please :)
My current table structure ....
CREATE TABLE download_stats (
id float NOT NULL default '0',
file_name varchar(10) default NULL,
d_date date default NULL,
hits float default NULL
);
current records
---------------------
id file_name d_date hits
1 file1.txt 2005-08-18 12
1 file1.txt 2005-08-19 4
2 file2.txt 2005-08-19 6
i executed the folooowing and got the following results....
Date & Hits for file 1
18/08/2005 12
19/08/2005 4
Total downloads of file1 =16
Date & Hits for file 2
19/08/2005 6
Total downloads of file2 =6
Total downloads of file1 and 2 =22
<?php
function mysql_date_format($date_va lue_,$date _format_)
{
//Find the separator for the date it should be either / or -
if(strpos($date_value_,"/" )>0)
$date_separator_="/";
else if(strpos($date_value_,"-" )>0)
$date_separator_="-";
$splitted_date_=split($dat e_separato r_,$date_v alue_);
//Find the separator for the date format it should be either / or -
if(strpos($date_format_,"/ ")>0)
$format_separator_="/";
else if(strpos($date_format_,"- ")>0)
$format_separator_="-";
$splitted_format_=split($f ormat_sepa rator_,$da te_format_ );
//Find the first element in the array and assign it to date or month or year
if($splitted_format_[0]==" dd")
$date_=$splitted_date_[0];
else if($splitted_format_[0]==" mm")
$month_=$splitted_date_[0] ;
else if($splitted_format_[0]==" yyyy" or $splitted_format_[0]=="yy" )
$year_=$splitted_date_[0];
//Find the second element in the array and assign it to date or month or year
if($splitted_format_[1]==" dd")
$date_=$splitted_date_[1];
else if($splitted_format_[1]==" mm")
$month_=$splitted_date_[1] ;
else if($splitted_format_[1]==" yyyy" or $splitted_format_[1]=="yy" )
$year_=$splitted_date_[1];
//Find the third element in the array and assign it to date or month or year
if($splitted_format_[2]==" dd")
$date_=$splitted_date_[2];
else if($splitted_format_[2]==" mm")
$month_=$splitted_date_[2] ;
else if($splitted_format_[2]==" yyyy" or $splitted_format_[2]=="yy" )
$year_=$splitted_date_[2];
return $year_."/".$month_."/".$da te_;
}
$con=mysql_connect("localh ost","root ","");
mysql_select_db("test");
// For file 1
$sel_query_today="select d_date,hits from download_stats where id=1";
$sel_query_today_results=m ysql_query ($sel_quer y_today);
$total1 =0;
echo "<br><br>Date & Hits for file 1";
While($row=mysql_fetch_arr ay($sel_qu ery_today_ results))
{
echo "<br>".mysql_date_format($ row['d_dat e'],"dd-mm -yyyy")." ".$row['hits'];
$total1 +=$row['hits'];
}
echo "<br>Total downloads of file1 =".$total1;
// For file 2
$total2 =0;
$sel_query_today="select d_date,hits from download_stats where id=2";
$sel_query_today_results=m ysql_query ($sel_quer y_today);
echo "<br><br>Date & Hits for file 2";
While($row=mysql_fetch_arr ay($sel_qu ery_today_ results))
{
echo "<br>".mysql_date_format($ row['d_dat e'],"dd-mm -yyyy")." ".$row['hits'];
$total2 +=$row['hits'];
}
echo "<br><br>Total downloads of file2 =".$total2;
echo "<br><br>Total downloads of file1 and 2 =". ($total2+$total1);
?>
I hope this is what you are looking for, you can customize this as you need.
CREATE TABLE download_stats (
id float NOT NULL default '0',
file_name varchar(10) default NULL,
d_date date default NULL,
hits float default NULL
);
current records
---------------------
id file_name d_date hits
1 file1.txt 2005-08-18 12
1 file1.txt 2005-08-19 4
2 file2.txt 2005-08-19 6
i executed the folooowing and got the following results....
Date & Hits for file 1
18/08/2005 12
19/08/2005 4
Total downloads of file1 =16
Date & Hits for file 2
19/08/2005 6
Total downloads of file2 =6
Total downloads of file1 and 2 =22
<?php
function mysql_date_format($date_va
{
//Find the separator for the date it should be either / or -
if(strpos($date_value_,"/"
$date_separator_="/";
else if(strpos($date_value_,"-"
$date_separator_="-";
$splitted_date_=split($dat
//Find the separator for the date format it should be either / or -
if(strpos($date_format_,"/
$format_separator_="/";
else if(strpos($date_format_,"-
$format_separator_="-";
$splitted_format_=split($f
//Find the first element in the array and assign it to date or month or year
if($splitted_format_[0]=="
$date_=$splitted_date_[0];
else if($splitted_format_[0]=="
$month_=$splitted_date_[0]
else if($splitted_format_[0]=="
$year_=$splitted_date_[0];
//Find the second element in the array and assign it to date or month or year
if($splitted_format_[1]=="
$date_=$splitted_date_[1];
else if($splitted_format_[1]=="
$month_=$splitted_date_[1]
else if($splitted_format_[1]=="
$year_=$splitted_date_[1];
//Find the third element in the array and assign it to date or month or year
if($splitted_format_[2]=="
$date_=$splitted_date_[2];
else if($splitted_format_[2]=="
$month_=$splitted_date_[2]
else if($splitted_format_[2]=="
$year_=$splitted_date_[2];
return $year_."/".$month_."/".$da
}
$con=mysql_connect("localh
mysql_select_db("test");
// For file 1
$sel_query_today="select d_date,hits from download_stats where id=1";
$sel_query_today_results=m
$total1 =0;
echo "<br><br>Date & Hits for file 1";
While($row=mysql_fetch_arr
{
echo "<br>".mysql_date_format($
$total1 +=$row['hits'];
}
echo "<br>Total downloads of file1 =".$total1;
// For file 2
$total2 =0;
$sel_query_today="select d_date,hits from download_stats where id=2";
$sel_query_today_results=m
echo "<br><br>Date & Hits for file 2";
While($row=mysql_fetch_arr
{
echo "<br>".mysql_date_format($
$total2 +=$row['hits'];
}
echo "<br><br>Total downloads of file2 =".$total2;
echo "<br><br>Total downloads of file1 and 2 =". ($total2+$total1);
?>
I hope this is what you are looking for, you can customize this as you need.
ASKER
looks great str_kani, ill test it now.
can you show me the correct way of adding hits for both files also please ?
thank you :)
can you show me the correct way of adding hits for both files also please ?
thank you :)
This is the download.php file which trackes this and give the file.
<?php
$con=mysql_connect("localh ost","root ","");
mysql_select_db("test");
if($_GET['file']==1)
{
$filename="file1.txt";
$file_id=1;
}
else if($_GET['file']==2)
{
$filename="file2.txt";
$file_id=2;
}
// for checking whether a record there for this ....
$check_query="select * from download_stats where d_date='".date('Y-m-d')."' and id=$file_id";
$result=mysql_query($check _query);
if($row=mysql_fetch_array( $result))
{
// record exists so update it.
$insert_query="update download_stats set hits=hits+1 where d_date='".date('Y-m-d') ."' and id=$file_id";
mysql_query($insert_query) ;
}
else
{
// no record for the current date so insert a record
$insert_query="insert into download_stats values($file_id,'".$filena me."','".d ate('Y-m-d ')."',1)";
mysql_query($insert_query) ;
}
header("Content-Type: application/x-octet-stream ");
header("Content-Dispositio n: attachment;filename=$filen ame");
readfile($filename);
exit;
?>
<?php
$con=mysql_connect("localh
mysql_select_db("test");
if($_GET['file']==1)
{
$filename="file1.txt";
$file_id=1;
}
else if($_GET['file']==2)
{
$filename="file2.txt";
$file_id=2;
}
// for checking whether a record there for this ....
$check_query="select * from download_stats where d_date='".date('Y-m-d')."'
$result=mysql_query($check
if($row=mysql_fetch_array(
{
// record exists so update it.
$insert_query="update download_stats set hits=hits+1 where d_date='".date('Y-m-d') ."' and id=$file_id";
mysql_query($insert_query)
}
else
{
// no record for the current date so insert a record
$insert_query="insert into download_stats values($file_id,'".$filena
mysql_query($insert_query)
}
header("Content-Type: application/x-octet-stream
header("Content-Dispositio
readfile($filename);
exit;
?>
ASKER
thank you i'll test it now
by the way, when i insert more records in i get this error:
Warning: split(): REG_EMPTY
works fine if theres just 1 record. what could be wrong ?
by the way, when i insert more records in i get this error:
Warning: split(): REG_EMPTY
works fine if theres just 1 record. what could be wrong ?
ASKER
nm nm, i needed to set the filename :)
but i still get error for the second file
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
strange
ok lemme test the hit adding now :)
but i still get error for the second file
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
strange
ok lemme test the hit adding now :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ahh.. i just messed up a name, nothing big hehe.
thank you str_kani, appreciate it ! Works great !! :)
i posted another, concerning this one:
https://www.experts-exchange.com/questions/21533831/followup-on-two-ways-of-doing-things.html
thank you str_kani, appreciate it ! Works great !! :)
i posted another, concerning this one:
https://www.experts-exchange.com/questions/21533831/followup-on-two-ways-of-doing-things.html
i would use the second one of updating the table based on the date.
I assume you are using the second way,