Results 1 to 6 of 6

Thread: help with read visits.txt file

  1. #1
    Join Date
    Jun 2007
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default help with read visits.txt file

    Hi,

    On my homepage i have a php script that writes the date (format: d/m/Y) to the text file "visits.txt".

    and i want a code on another page that will open the text file and count each date.

    e.g.

    visits.txt

    01/04/2010, 01/04/2010, 01/04/2010, 01/04/2010, 29/03/2010, 29/03/2010

    output:

    01/04/2010 = 4
    29/03/2010 = 2

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Put this on the page you're counting dates:
    Code:
    <?php
    $file = fopen('date.txt', 'a');
    fwrite($file, " ".date("d/m/Y"));
    fclose($file);
    ?>
    Make a blank txt file named date.txt, and then where you want to add up all the dates, put this:
    Code:
    <?php
    $file = file_get_contents("date.txt");
    $file = explode(" ", $file);
    array_shift($file);
    $dates = array();
    foreach($file as $key => $value){
      if(!isset($dates[$value])){
        $dates[$value] = 1;
      } else {
        $dates[$value]++;
      }
    }
    foreach($dates as $key => $value){
      echo $key . ' = '.$value .'<br />';
    }
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    vinny.benson (04-01-2010)

  4. #3
    Join Date
    Jun 2007
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    yey, thank you so much that worked perfectly

  5. #4
    Join Date
    Jun 2007
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    sorry just one more question

    in this code, how would I get the date with the highest count

    PHP Code:
    <?php
    $file 
    file_get_contents("date.txt");
    $file explode(" "$file);
    array_shift($file);
    $dates = array();
    foreach(
    $file as $key => $value){
      if(!isset(
    $dates[$value])){
        
    $dates[$value] = 1;
      } else {
        
    $dates[$value]++;
      }
    }
    foreach(
    $dates as $key => $value){
      echo 
    $key ' = '.$value .'<br />';
    }
    ?>
    basically i want to output this:

    01/04/2010 = 4
    29/03/2010 = 2

    most views on: 01/04/2010 (4)

  6. #5
    Join Date
    Jun 2007
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    doesn't matter, i figured it out

    PHP Code:
    <?php
    $max 
    max($dates);
    echo 
    'most views: ' $max;
    ?>

  7. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No problem, glad to help.

    It seems your topic is solved... Please set the status to resolved.. To do this:
    Go to your first post ->
    Edit your first post ->
    Click "Go Advanced" ->
    Then in the drop down next to the title, select "RESOLVED"
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •