Why doesnt readfromfile statement read all lines from a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Why doesnt readfromfile statement read all lines from a file

Post 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?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

sorry mate that doesn't seem to be workin, nice try though :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>"); 

?>
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
Post Reply