what am i doing wrong ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

what am i doing wrong ???

Post by dannyd »

In the asrunlogs directory there is 5 files:
081902XXX.txt
082002XXX.txt
082102XXX.txt
082202XXX.txt
082302XXX.txt

Im trying to loop once through all these files and output the columns, but my code doesnt seem to loop logically. Anyone know what I could be doing wrong here ?

**** PLEASE USE THE

Code: Select all

TAG WHEN POSTING *****[/color]

Code: Select all

<?PHP
//date format example 082102XXX.txt is February 21, 2007 OR  Year-Day-Month
//read file name in directory
       $today = date("ydm");
       $CURRENT_YEAR = substr($today,0,2); //08
       $CURRENT_DAY  = substr($today,2,2); //21
       $CURRENT_MONTH  = substr($today,4,2); //02
 
  for ($i=0;$i<5;$i++) {
 
             $filename = $CURRENT_YEAR . $CURRENT_DAY+$i . $CURRENT_MONTH . 'XXX.txt';    
           
        //Open asrunlogs directory
            $dir = @ opendir("./asrunlogs");
 
            //List files in asrun directory directory
    while (($filename = readdir($dir)) !== false && $filename != '.' && $filename != '..') {
             
            echo '<b>Displaying contents of' . $filename . '</b><BR>';
 
        //parse contents in file
             $file_handle = file_get_contents('./asrunlogs/' . $filename);
     
             // Explodes .txt file into lines (\n)
              $Lines = explode("\n",$file_handle);
 
                  //display fields from file     
                    foreach($Lines as $Line) {
                                            
            $Line = preg_replace('/\s\s+/', ' ', trim($Line));
                    $fields = explode(" ",$Line);
            
                     if ($Line != 'XXX As Run.txt'){ 
                
            echo $fields[1] . '&nbsp;' . $fields[2] . '&nbsp;' . $fields[3] . '<BR>';
                      }
 
                }
 
        }
    
  }
  closedir($dir);
   
       
?>
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: what am i doing wrong ???

Post by Stryks »

It's always a good idea to post exactly HOW it isn't working. Error message? No Output? Unexpected output - what did you expect and what did you get?

It just help us to know how to help.

In your case I can see three main issues.

1. You are changing directory in each loop iteration. As a result, you will only ever read the '.' folder. So relocate ...

Code: Select all

$dir = @ opendir("./asrunlogs");
to above the for statement.

2. Your method of adding $i to the $CURRENT_DAY isn't working. Not sure of the reason, but when adding a value as part of an evaluation, it helps to put the sum in brackets. The following works ...

Code: Select all

$filename = $CURRENT_YEAR . ($CURRENT_DAY+$i) . $CURRENT_MONTH . 'XXX.txt';
3. This isn't an error, just an oddity. You get the current date, break it up, and set $filename using those values, and then you do ...

Code: Select all

while (($filename = readdir($dir)) !== false && $filename != '.' && $filename != '..') {
... which gives $filename the return value of readdir($dir), overwriting the value you set. I'm not really sure how you were planning on using it, but it's gone before you get a chance to use it.

Hope this helps. :)
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: what am i doing wrong ???

Post by dannyd »

Thanks for your help!
Post Reply