?
Solved

Unexpected output for my code and append issues

Posted on 2016-09-05
5
Medium Priority
?
51 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 111

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 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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