Page 1 of 1

creating a comment script

Posted: Wed May 13, 2009 6:46 am
by misfitxnet
am working on a script to submit and display comments on a web page.
The way it is setup is that the user writes comments in two text inputs, and ithe script submits the output to a textfile.
I am having trouble though. So far, I can not submit the data so that it creates a new line for each submission. The code I am using is:

Code: Select all

 
<?php
 
$name = $_POST['name'];
$comment = $_POST['comment'];
 
 
$output = $name."\t".$comment."\t"."\n";
 
$fp = fopen("d:\hshome\c239198\local54memberforum.com\info.txt", "ab");
fwrite($fp, $output);
 
 
 
?>
 
This will work, except for the \n.
The output created is a single line in info.txt.

So i try and work with this to display the content of output.txt
First I make it into a string, and then try to manipulate it using:

Code: Select all

 
<?php
             
                                    $commenttext=file_get_contents("d:\hshome\c239198\local54memberforum.com\info.txt");
                                    list($name, $comment) = split("\t", $commenttext);
              
                        echo "<tr bgcolor=6699CC>". 
                        "<td>Name: $name</td>\n"."</tr>".
                                       "<tr bgcolor=0066CC>".
                                       "<td>Comment: $comment</td>\n"."</tr>\n";
 
 
               
                        ?>
 
 
But this will only return the first entry, and nothing else.

My question is, how do I set this up to display each entry in a seperate table row, every time
someone submits something?

Is there a better way to format the text file?
Is using list() and split() my best options?

Thanks

Re: creating a comment script

Posted: Wed May 13, 2009 6:53 am
by divito
What is the reason you're using a text file instead of a database?

Re: creating a comment script

Posted: Wed May 13, 2009 7:02 am
by misfitxnet
this was easier for me

Re: creating a comment script

Posted: Wed May 13, 2009 7:48 am
by Defiline

Code: Select all

<?php
 
/**
 * This file writes data into file
 */
 
/**
 * Getting user data
 */
$name    = trim(htmlspecialchars(substr($_POST['name'], 0, 64)));
$comment = trim(htmlspecialchars(substr($_POST['name'], 0, 1000)));
 
$output = "{$name}\t{$comment}\t\n";
 
/**
 * Writing a file
 */
$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/info.txt', 'a+');
 
fwrite($handle, $output);
fclose($handle);
 
?>

Code: Select all

<?php
 
$comments = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/info.txt');
 
list($name, $comment) = split("\t", $comments);
 
$comment = str_replace("\n", '<br />', $comment);
 
echo '
    <tr bgcolor=6699CC>
        <td>Name: '.$name.'</td>
    </tr>
    <tr bgcolor=0066CC>
       <td>Comment: '.$comment.'</td>
    </tr>';
 
?>

Re: creating a comment script

Posted: Sat Jun 13, 2009 12:22 pm
by mwalimo
misfitxnet wrote: So far, I can not submit the data so that it creates a new line for each submission.
I am having exactly the same problem. My platform is Windows VISAT, WAMP server.

Re: creating a comment script

Posted: Sat Jun 13, 2009 1:20 pm
by misfitxnet
I did not get a solution, I wound up using a database instead.
The output file never recognized /n as a new line. I believe it may have even seen it as a tab space instead, I do not know why.
Good luck if I figure this one out, I will post on here. I'll be sure to work on it.

Re: creating a comment script

Posted: Sat Jun 13, 2009 1:29 pm
by mwalimo
Were /are you using the Windows & WAMP environment? That is what I am using. Could it be that \n does not work on the Windows/WAMP platform?

Re: creating a comment script

Posted: Sat Jun 13, 2009 2:05 pm
by mwalimo
Check this conversation:
QUESTION (our exact problem!)
Hi People,
If anyone can help me please?
Im trying to write to a txt file and create a new line break from the code i submit.
This is what i'm using as an example:
$myFile = "test.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby\n";
fwrite($fh, $stringData);
$stringData = "Pointy\n";
fwrite($fh, $stringData); fclose($fh);
But when i open the .txt file i end up with:
BobbyPointy

Any suggestions please?
Cheers!
ANSWER
Are you on a windows system?
If you are, you may need to write
$string = "Bobby\r\n";
As windows handles new lines a bit differently. Try that and see if it works

FEEDBACK (But it is not working for me on my Windows VISTA)
Brilliant, thanks for the quick response! working

Re: creating a comment script

Posted: Sat Jun 13, 2009 2:28 pm
by mwalimo
Here is the solution:

replace \n with \r\n
....................
....................
fwrite($filein, "$name\r\n");
fwrite($filein, "$email\r\n");
....................
....................

BTW wordpad ( not notepad !!) shows the text the right way with \n