Jump to content

[SOLVED] fgets() not reading line by line?


Recommended Posts

Hello everyone!

I'm pretty decent with PHP, however I am having some trouble with a guestbook script.

My friend wants all comments to be stored within a single text file, and then output only 1 comment per page. Not sure why.

 

Anywho, I have everything working, up until I have it read the lines within the file and output how many times it finds the text, "Newline", which would denote a new comment. The text file has Newline written in it several times, each on their own lines, but the script is always telling me that there are no instances of a line containing the text, "Newline".

 

Here is the last bit of code dealing with the file creation, I'm not sure what is wrong with it.

 


//Write out the guestbooks contents

$guestbook=fopen("guestbook.txt","r");
while(!feof($guestbook))
{

//Echo the contents of the file
echo "<br/>";
echo fgets($guestbook);

}
fclose($guestbook);



//echo the number of <br/> I put in a tag.
$guestbook=fopen("guestbook.txt","r");
while(!feof($guestbook))
{
$num;
if(fgets($guestbook)=="Newline")
{
$num++;
}

}
fclose($guestbook);
echo $num;



//Writing to a file
if(isset($_POST["submit"]))
{
$guestbook=fopen("guestbook.txt","a");
fwrite($guestbook,"Newline");
//Creates a new line
fwrite($guestbook,"
");
fwrite($guestbook,$_POST["input"]);
//Creates a new line
fwrite($guestbook,"
");
fclose($guestbook);
}

Link to comment
https://forums.phpfreaks.com/topic/82854-solved-fgets-not-reading-line-by-line/
Share on other sites

If you want to create a newline you should use \n

<?php
fwrite($guestbook,"\n");

//get line count
$num = 0;
while(!feof($guestbook))
{
    //$num; this is not needed inside the loop, initiate it before the loop
    $num++;
}
echo $num;

//you can put it in the first loop
while(!feof($guestbook))
{
    ++$num;
    //Echo the contents of the file
    echo "<br/>";
    echo fgets($guestbook);

}
echo $num;
?>

 

Thank you for the response, but that isn't quite what I meant when I said counting the "Newlines".

 

Here is the contents of guestbook.txt -


Newline
Testing...
Newline
Why does it read 0?
Newline
Hm...
Newline
Wut

 

Newline is used to denote a new comment. My script should output 4, since there are 4 instances of "Newline", however, it always outputs 0.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.