DannyM Posted December 22, 2007 Share Posted December 22, 2007 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); } Quote Link to comment https://forums.phpfreaks.com/topic/82854-solved-fgets-not-reading-line-by-line/ Share on other sites More sharing options...
Daukan Posted December 22, 2007 Share Posted December 22, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82854-solved-fgets-not-reading-line-by-line/#findComment-421391 Share on other sites More sharing options...
DannyM Posted December 23, 2007 Author Share Posted December 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82854-solved-fgets-not-reading-line-by-line/#findComment-421397 Share on other sites More sharing options...
Daukan Posted December 23, 2007 Share Posted December 23, 2007 Try this <?php $num = 0; while(!feof($guestbook)) { if(strpos(fgets($guestbook), 'Newline') ) { $num++; } } echo $num; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82854-solved-fgets-not-reading-line-by-line/#findComment-421403 Share on other sites More sharing options...
Barand Posted December 23, 2007 Share Posted December 23, 2007 you need if(strpos(fgets($guestbook), 'Newline') !== false) as in this instance strpos will return 0 (beginning of the string) and so condition is false. Quote Link to comment https://forums.phpfreaks.com/topic/82854-solved-fgets-not-reading-line-by-line/#findComment-421405 Share on other sites More sharing options...
DannyM Posted December 23, 2007 Author Share Posted December 23, 2007 Hey, thanks! It works now! Now, how to say the topic is finished.. Quote Link to comment https://forums.phpfreaks.com/topic/82854-solved-fgets-not-reading-line-by-line/#findComment-421408 Share on other sites More sharing options...
Daukan Posted December 23, 2007 Share Posted December 23, 2007 you need if(strpos(fgets($guestbook), 'Newline') !== false) as in this instance strpos will return 0 (beginning of the string) and so condition is false. Thanks. Good looking out. Quote Link to comment https://forums.phpfreaks.com/topic/82854-solved-fgets-not-reading-line-by-line/#findComment-421421 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.