Link to home
Start Free TrialLog in
Avatar of SaLz
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,'".$fileName."','".date('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
Avatar of Kani Str
Kani Str
Flag of India image

The second option of updating looks better, this will keep then number of records smaller in the tables and you can easily find the totals...

i would use the second one of updating the table based on the date.

I assume you are using the second way,
Avatar of SaLz
SaLz

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 :)
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_value_,$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($date_separator_,$date_value_);
      
      //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($format_separator_,$date_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_."/".$date_;
}


$con=mysql_connect("localhost","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=mysql_query($sel_query_today);
$total1 =0;
echo "<br><br>Date  &   Hits for file 1";
While($row=mysql_fetch_array($sel_query_today_results))
{
      echo "<br>".mysql_date_format($row['d_date'],"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=mysql_query($sel_query_today);
echo "<br><br>Date  &   Hits for file 2";
While($row=mysql_fetch_array($sel_query_today_results))
{
      echo "<br>".mysql_date_format($row['d_date'],"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.
Avatar of SaLz

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 :)
This is the download.php file which trackes this and give the file.

<?php
$con=mysql_connect("localhost","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,'".$filename."','".date('Y-m-d')."',1)";
      mysql_query($insert_query);
}
      header("Content-Type: application/x-octet-stream");
      header("Content-Disposition: attachment;filename=$filename");
      readfile($filename);
      exit;
?>
Avatar of SaLz

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 ?
Avatar of SaLz

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 :)
ASKER CERTIFIED SOLUTION
Avatar of Kani Str
Kani Str
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SaLz

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