Unexpected output for my code and append issues

<?php 

$find = '.5010.';

$dirname = 'rename';
if(!is_dir($dirname))
    mkdir($dirname, 0777);

$directory_with_files = './';
$dh  = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
    if(in_array($filename, array('.', '..')) || is_dir($filename))
        continue;

    $files[] = $filename;
}


foreach($files as $file)
{
    //find only 5010 files
    if(stripos($file, $find) !== false)
    {
        // open the 5010 file
        $handle = fopen($file, "r");
        $file_content = file_get_contents($file);
     
        
        if ($handle) {
            $header = '';
            $name = '';
            $footer = '';
            $payor_blocks = array();
            
            
            // determine if file has more than one payor
            $payor_count = substr_count($file_content, 'N1');
            //if the file has more than one payor
            if($payor_count > 1) {
                //read the file line by line
                $header_end = false;
                $block_start = false;
                $count = 1;
 //payor_count outputs: 5
 //header_end and block_start prints out nothing
           
             
                
                //look for occurances of CAT and what line each on is on
                $line_number = 0;
                $line_stop= array();
                   while (($line = fgets($handle)) !== false) {
            
                    $line_number++;
                  
                    if(strpos($line, 'CAT') !==false){
                    	
                    $line_stop[] = $line_number;
                    }
                  
                   }
                   $footer_line = count($line_stop)-2;
                   $footer_line = $line_stop[$footer_line];
                  
                  
                $line_number = 0;
                while (($line = fgets($handle)) !== false) {
                    $line_number++;
                   

                    //look for the first payor block
                    if(strpos($line, 'N1') !== false || $block_start) {
                        $header_end = true; $block_start = true;
                         if(strpos($line, 'N1') !== false) {
                         	$count++;
                         }  
                            
                        //see if the block finished
                         if($line_number == $footer_line) {
                         	$block_start = false;
                         	// $footer .= $line."\n";
                         }
                         
                       // $payor_blocks[$count] .= $line;
                    } else {
                        //append to the header
                        if($header_end) {
                            $footer .= $line."\n";
                           
                        } else {
                            $header .= $line."\n";
                        }
                    }
      
                }
               
                //get payor blocks and create a file foreach payor
                $new_files = array();
                foreach($payor_blocks as $block) {
                    $filename = $file . "_" . $count;
                    $count++;
                    $new_files[] = array(
                        'name' => $filename,
                        'content' => $header."\n".$block."\n".$footer	
                    	
                    );
                  
                    
//footer prints out after the first CAS and all the rest
                }
                echo "<pre>" . print_r($new_files, true) . "</pre>";
                //loop through new files and create them
                foreach($new_files as $new_file) {
                    //create file
                	$myfile = fopen($filename, "w");
   
                    //put contents in the file               	
                	fwrite($filename, $new_file['content']);	
              		//close the file
                	fclose($myfile);
                ?>

Open in new window


After the first while loop anything I print_r at , gives me this out put:
Array(
)
Did I miss something?
Also I want it to print out whatever is after CAT in the footer.
LVL 1
Jazzy 1012Asked:
Who is Participating?
 
Kim WalkerConnect With a Mentor Web Programmer/TechnicianCommented:
By the way, in those while loops that read through the open file, you are searching for the strings CAT and N1 anywhere within the line. I would expect that CAT might frequently appear in the middle or end of the line which would satisfy the condition of the strpos !== false statement. Although N1 is less likely to appear at random locations within the lines, it is still possible. I suggest you change your search to look for lines that begin with CAT or N1.
if(strpos($line, 'CAT') === 0){

Open in new window

This will only recognize lines where the search string is exactly at position zero in the line.
1
 
Chris HarteThaumaturgeCommented:
The keyword "continue" does the same job as break. It will take you out of the while loop before the $files variable is  populated.
0
 
Ray PaseurCommented:
Could you please post a test data set showing us the information you're working with?  Sorry to keep asking for this over and over, but it's necessary for us to be able to post tested examples.  And if we have the test data in the same post with the question it's much easier to keep things organized.  

Thanks.
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Your first while loop is on line 12. But I don't think that's the while loop you are referring to.

Your next while loop is on line 54 and reads through the open file. At the completion of the while loop, the file pointer is at the end of the file.

The next while loop is on line 69 and reads through the open file again. However, the file pointer is still at the end of the file, so it has nothing to read. You need to rewind the file pointer to the beginning of the file after line 63 and before line 69.
rewind($handle);

Open in new window

0
 
Ray PaseurConnect With a Mentor Commented:
We don't really know how big these files are (the test data will help us see), but it might be easier to read each entire file into an array with file(), instead of reading and re-reading to process each line one at a time.  Just a thought...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.