Page 1 of 1

really, REALLY easy question.

Posted: Sun Nov 23, 2008 3:08 pm
by Suudsu
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi, my first time posting.

So, I am just starting off PhP and I am trying to make some code where a loop reads some data in a file, and continues reading until it comes across a blank line in a file. The problem is, nothing prints out. Have fun picking out all errors I have made.

Code: Select all

<?php
$myFile = "file.txt";
$fh = fopen($myFile, 'r');
$Line = fgets($fh)."<br>";
while ($Line != "<br>") {
$All = $All.$Line
$Line = fgets($fh)."<br>";
}
fclose($fh);
echo $All;
?>
file.txt is....

Code: Select all

Hello Sire
I am a squire.
 
Do not see me.

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: really, REALLY easy question.

Posted: Sun Nov 23, 2008 3:35 pm
by Mark Baker
Missing semi-colon after

Code: Select all

$All = $All.$Line

Code: Select all

$Line = fgets($fh)."<br>";
while ($Line != "") {
 
$Line will never be != "" because you're always appending "<br>" to it before doing the test, so an empty line in the file will still contain "<br>" at the point when you test it.

Code: Select all

 
<?php
$All = '';
$myFile = "file.txt";
$fh = fopen($myFile, 'r');
$Line = fgets($fh);
while ($Line != "") {
   $All = $All.$Line."<br>";
   $Line = fgets($fh);
}
fclose($fh);
echo $All;
?>
 
 

Re: really, REALLY easy question.

Posted: Sun Nov 23, 2008 3:51 pm
by Suudsu
I actually caught the <br> always being there right and edited it as you posted.
Thanks on the semi-colon thing.

Unfortunately the problem persists, any ideas?

Re: really, REALLY easy question.

Posted: Sun Nov 23, 2008 4:11 pm
by Mark Baker
Suudsu wrote:Unfortunately the problem persists, any ideas?
No obvious ones, it works for me.

The only thing I have changed is

Code: Select all

 
while (trim($Line) != "<br>") {
 
because a seemingly blank line may still have spaces in.

Double check that file.txt is in the right directory (the same directory as your PHP), and possibly prefix the file with './' to ensure that it is using the correct directory.

Code: Select all

 
$myFile = "./file.txt";
 

Re: really, REALLY easy question.

Posted: Sun Nov 23, 2008 4:23 pm
by Suudsu
To make sure when I fixed it I didn't mess up anything, this is what I have got....

Code: Select all

<?php
$All = '';
$myFile = "file.txt"
$fh = fopen($myFile, 'r');
$Line = fgets($fh)."<br>";
while (trim($Line) != "<br>") {
   $All = $All.$Line;
   $Line = fgets($fh)."<br>";
}
fclose($fh);
echo $All;
?>
Could you post the code that works for you, so I can see if its a problem with the code or not?

Re: really, REALLY easy question.

Posted: Sun Nov 23, 2008 4:30 pm
by Mark Baker
Missing semi-colon after

Code: Select all

$myFile = "file.txt"

Re: really, REALLY easy question.

Posted: Sun Nov 23, 2008 4:36 pm
by Suudsu
Success! Thanks a ton.