Click to See Complete Forum and Search --> : PHP: I can't get this to work!


slacker_x
04-13-2002, 04:53 AM
This script is going into an infinite loop on this line: while($line != ""). The final line in the text file is a blank line with a \n character. the rtrim should remove that and put an empty variable in the array. I thought it might be the emptiness that was causing the problem, so I terminated the messages with a "3" instead. Still the same infinite loop. Any idea why this is happening?


<?php
$fp = fopen("guestbook.txt", "r");
$file_array = array();
while(!feof($fp))
{
$buffer = fgets($fp, 8192);
if(!feof($fp))
{
$file_array[] = rtrim($buffer);
}
}
fclose($fp);

$message = array();
foreach($file_array as $line)
{
while($line != "")
{
$message[] = $line;
}
print("NAME: ".array_shift($message)."<br ?>\n");
print("EMAIL: ".array_shift($message)."<br ?>\n");
foreach($message as $msg_line)
{
print($msg_line);
}
}

?>

Whipping Boy
04-14-2002, 12:10 PM
Try replacing "" with EOF

Sean111
04-14-2002, 08:50 PM
Yeah it keeps reading on and on cause it isn't set to stop at the EOF, so thus adding EOF instead of "" should work

slacker_x
04-14-2002, 09:14 PM
I don't think you're right. First of all because replacing "" with EOF didn't help, but secondly because I already test for EOF at the top of the code.

element-x
04-15-2002, 12:40 AM
EOF isn't the way to go about this, first, he's going through an array that he populated while the file was open, and obviously detected EOF at that point.

Try trim() instead of rtrim()...see if that does anything. I don't think it will, but it's possible.

not sure if this would work but you could try...

while (empty($line))
{
...

I highly doubt it'll work, but it is possible.

or...

while (strlen($line) == 0)
{
...

These get uglier and uglier as I go, so I'll stop now.

slacker_x
04-16-2002, 04:59 AM
I still haven't figured it out...

tried all of the suggestions.