text file descending order

Discussion in 'PHP' started by saiha, Oct 23, 2006.

  1. #1
    helo gud day to you all.. i need some help please i hope you can help.
    i making a system for my office and the system i make i put some script that can records all the activities doing by the user .. for example he/she change his/her password, when he submit the update form the logs will automatically created a text file . and the logs can be found on my text file.

    when i view the logs activity i can i see what they to on that day. my problem is how to view the logs when descending order. so that the new logs will be on the top when i view the logs activity.

    thanks in advance i hope you can help me in my problem..
     
    saiha, Oct 23, 2006 IP
  2. Nickwiz

    Nickwiz Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If i understand you correctly you want to add new entries to beginning of file.

    There is a similar thread here:
    http://forums.digitalpoint.com/showthread.php?t=157960

    You could try something like:

    
    
      // With file_get_contents()
    
      // $file = file to write to
      // $data = data to write
      $file = 'log.txt';
      $data = date("H:i:s",time())."\tThis is some \n text; foo was here.\n";
    
      // Creates file if do not exist, else open for write
      $log  = fopen($file,is_file($file)?"r+":"w+");
    
      // Output new data and old data
      fwrite($log,$data.file_get_contents($file));
      // Close fileh
      fclose($log); 
    
    
    PHP:
     
    Nickwiz, Oct 23, 2006 IP
  3. saiha

    saiha Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    this is the code i use to view the logs.txt.. what i want is when i click the view logs the logs will be on descending order so that it easy to know the new log entry.. i hope you can help me.. thanks in advance Nickwiz

    $file = "logs.txt";
    $handle = fopen($file , "r");
    $size = filesize($file);

    $content =fread($handle,$size);
    fclose($handle);

    echo "$content";
     
    saiha, Oct 23, 2006 IP
  4. Nickwiz

    Nickwiz Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Depends.
    Are there one entry per line?
    If so you can read the whole file into an array, reverse the array and then output it.

    I.e.:
    
    <?php
    
      $file = "logs.txt";
      // file() (PHP 3, PHP 4, PHP 5)
      // read the file into array
      $ar_log = file($file);
      // flip the array
      $ar_log = array_reverse($ar_log);
      // convert array to string
      $log    = implode('',$ar_log);
      // print result
      echo '<pre>'.$log.'</pre>';
    
    ?>
    
    PHP:
    Can be rewritten as;

    <?php

    echo '<pre>'.implode('',array_reverse(file('logs.txt'))).'</pre>';

    ?>

    If you want do formatting per line you can do it by loop instead;

    
    <?php
    
      $ar_log = file('logs.txt');
      for($i=count($ar_log)-1;$i>=0;$i--){
        // do something with $ar_log[$i]
        echo $ar_log[$i];
      }
    
    ?>
    
    PHP:
    If you want to filter out entries you can use preg_grep
    I.e.: say each line are represented by dd.mm.yyyy{TAB}logentry

    You can do this for displaying only todays entries:
    
    <?php
    
      if(function_exists('date_default_timezone_set'))
          date_default_timezone_set("America/New_York");
      $ar_log = array_reverse(file('logs.txt'));
      $ar_log = preg_grep("/".date("d\.m\.Y",time()).".*/",$ar_log);
      echo '<pre>'.implode('',$ar_log).'</pre>';
    
    ?>
    
    PHP:
    This way you can also make a simple filter i.e:

    
    <?php
    
      if(function_exists('date_default_timezone_set'))
          date_default_timezone_set("America/New_York"); // cahnge to your timezone
    
      $ar_log = array_reverse(file('logs.txt'));
      $filter = preg_replace("/([0-9]{2})\.([0-9]{2})\.([0-9]{4})(.*)/","<li><a href='".$_SERVER['PHP_SELF']."?show_date=\\1\.\\2\.\\3'>\\1.\\2.\\3</a></li>",$ar_log);
      $filter = array_flip(array_flip($filter));
    
      echo '<ul>'.implode('',$filter);
      echo "<li><a href='".$_SERVER["PHP_SELF"]."?show_date='>Show All</a></li></ul><hr>";
    
      $show_date = isset($_GET["show_date"]) ? $_GET["show_date"] : date("d\.m\.Y",time());
      $ar_log = preg_grep("/".$show_date.".*/",$ar_log);
      echo 'Log for: '.str_replace('\\','',($show_date==''?'All dates':$show_date)).'<pre>'.implode('',$ar_log).'</pre>';
    
    ?>
    
    PHP:
    Witch will output (if link 23.10.2006 are clicked);

    • 24.10.2006 (as link)
    • 23.10.2006 (as link)
    • 22.10.2006 (as link)
    • Show All (as link)
    ---------------------------------------------------------------
    Log for: 24.10.2006

    23.10.2006 Log entry
    23.10.2006 Log entry


    **

    And so on. Depending on format in logfile you can do all sorts of filtering... ie users and so on.
    Best is of course to use a DB, but if you do not have access to any a flat file is a solution.
    Consider using one file per day, per week, per user or so if the log get long...

    If you give the format of the file it is easier to give examles / solve.
     
    Nickwiz, Oct 24, 2006 IP
  5. saiha

    saiha Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks you for the replay Nickwiz and the information about descending order in textfile.. i got it all now Nickwiz thanks alot it really help.. thanks and GOD BLESS... :D
     
    saiha, Nov 3, 2006 IP