Solved

Unexpected output for my code and append issues

Posted on 2016-09-05
5
38 Views
Last Modified: 2016-09-09
<?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.
0
Comment
Question by:Jasmine Ikhreishi
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 41784647
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41784793
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
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41784932
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
 
LVL 22

Accepted Solution

by:
Kim Walker earned 250 total points
ID: 41784942
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
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 41785001
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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question