Solved

Unexpected output for my code and append issues

Posted on 2016-09-05
5
27 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 16

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 108

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 21

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 21

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 108

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now