creating a comment script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
misfitxnet
Forum Newbie
Posts: 14
Joined: Mon May 11, 2009 8:40 am

creating a comment script

Post 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
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: creating a comment script

Post by divito »

What is the reason you're using a text file instead of a database?
misfitxnet
Forum Newbie
Posts: 14
Joined: Mon May 11, 2009 8:40 am

Re: creating a comment script

Post by misfitxnet »

this was easier for me
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: creating a comment script

Post 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>';
 
?>
mwalimo
Forum Newbie
Posts: 5
Joined: Sat Jun 13, 2009 11:42 am

Re: creating a comment script

Post 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.
misfitxnet
Forum Newbie
Posts: 14
Joined: Mon May 11, 2009 8:40 am

Re: creating a comment script

Post 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.
mwalimo
Forum Newbie
Posts: 5
Joined: Sat Jun 13, 2009 11:42 am

Re: creating a comment script

Post 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?
mwalimo
Forum Newbie
Posts: 5
Joined: Sat Jun 13, 2009 11:42 am

Re: creating a comment script

Post 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
mwalimo
Forum Newbie
Posts: 5
Joined: Sat Jun 13, 2009 11:42 am

Re: creating a comment script

Post 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
Post Reply