Page 1 of 1

Why doesnt readfromfile statement read all lines from a file

Posted: Sun Jun 27, 2004 10:20 am
by andylyon87

Code: Select all

<?php
print("<TR><TD colspan=5><textarea name="editing" cols=60 rows=10 NOWRAP>");
if($edit){
for($n = 0; $n<count($edit); $n++){
         $TheFile = "form_data/$edit[$n]";
         $Open = fopen ($TheFile, "r+");
         if($Open){
         $data = file($TheFile);
               $getline = explode("\t",$data[$n]);
                print("$getline[0]\n$getline[1]");
                fclose($Open);
         }else{
print("Unable to open a file<BR>\n");
               }
}}
print("</TEXTAREA>");
?>
Ok guys why doesnt this read all the lines in the file, have used same one before and has worked but this is inside a textarea although that shouldnt really make much difference, should it?

Posted: Sun Jun 27, 2004 10:40 am
by scorphus
I don't think it has something to do with textarea.

This should be reading only the $n-th line of each file since you're exploding only the $n-th line. Try implement another for loop to read every line. Something like this:

Code: Select all

<?php
print("<TR><TD colspan=5><textarea name="editing" cols=60 rows=10 NOWRAP>");
if($edit){
	for($n = 0; $n<count($edit); $n++){ // could also use a [php_man]foreach[/php_man]
		$TheFile = "form_data/$edit[$n]";
		$Open = fopen ($TheFile, "r+");
		if($Open){
			$data = file($TheFile);
			for($i = 0; $i<count($edit); $i++){
				$getline = explode("\t",$data[$i]);
				print("$getline[0]\n$getline[1]");
			}
			fclose($Open);
		}else{
			print("Unable to open a file<BR>\n");
		}
	}
}
print("</TEXTAREA>");
?>
-- Scorphus

Posted: Mon Jun 28, 2004 11:07 am
by andylyon87
sorry mate that doesn't seem to be workin, nice try though :)

Posted: Mon Jun 28, 2004 12:35 pm
by feyd
why use fopen() when you aren't using fread/fwrite?

Code: Select all

<?php

print("<TR><TD colspan=5><textarea name="editing" cols=60 rows=10 NOWRAP>"); 
if($edit){ 
   foreach($edit as $k => $n){ // could also use a foreach 
      $TheFile = "form_data/$n"; 
      if(is_readable($TheFile)){ 
         $data = file($TheFile); 
         for($data as $line){ 
            $getline = explode("\t",$line); 
            print("$getline[0]\n$getline[1]<br />\n"); 
         } 
      }else{ 
         print("$TheFile isn't readable or doesn't exist.<br />\n"); 
      } 
   } 
} 
print("</TEXTAREA>"); 

?>

Posted: Tue Jun 29, 2004 10:26 am
by scorphus
andylyon87 wrote:sorry mate that doesn't seem to be workin, nice try though :)
Humm... let me try some more: can you please post the rest of the code?

Thanks,
Scorphus.