Jazzy 1012
asked on
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);
?>
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.
The keyword "continue" does the same job as break. It will take you out of the while loop before the $files variable is populated.
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.
Thanks.
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.
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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.