view text file in php<

windows

Guest
I'm learning php and want to make a chat box thing like tag-board.com

I want to do it by have it save the posts to a text file say, tags.txt
How would i have the php show the text in the file with the name attached. Like i have a name input box, and a post input box. It would have to use variables for both I think. SO the tag would show up like this:
UserName: Tag message here
You would enter the name: $name and tag: $tag
then it would have to display like

print ("$name: $tag");

But i dont know how to have it open viiw and change the text file or if this is even possible. I think I could use My SQL also but i dont know how so im thinking text file would be easier. And it would have to save with the old m,essages also. This seems complicated but I hope its not that bad. Any tips?If you're using a text file you'll need to use the fopen, fwrite, and a few other functions that handle flat files.

I think it would be much easier to write to and manage using a database though.

It may also help to view an already made script and use it as an example.

Here's a good one to look at:
<!-- m --><a class="postlink" href="http://www.alphibia.com/ShoutIt.phpactually">http://www.alphibia.com/ShoutIt.phpactually</a><!-- m --> a text file is a lot harder.Then what would be the easy alternative?Databases.Well how is that done with phpHere's a list of mysql functions, I'm assuming your server supports mysql.
<!-- m --><a class="postlink" href="http://us2.php.net/manual/en/ref.mysql.phpWow">http://us2.php.net/manual/en/ref.mysql.phpWow</a><!-- m --> that's extremely confusing I don't know what they do or which one I want. here is my code:

<html>
<head>
<title>Chat</title>
</head>
<body>
<table align="center" border="0" width="200">
<tr>
<td width="200" height="20">
<?php

print ("Tag Board");

?>
</td>
</tr>
<tr>
<td height="200">
<?php

print ("This is where the tags would be view, the part I dont know how to do.");

?>
</td>
</tr>
<tr>
<td height="100">
Name:<br>
<form action="tags.php" method=post>
<input type="text" name="name"><br>
</td>
</tr>
<tr>
<td height="100">
Message:<br>
<input type="text" name="tag"><br>
</form>
</td>
</tr>
</table>
</body>
</html>

There are obvious problems. I just put that text where the viewing of the tags should go. And the name and message wont work either because it would over right the last one if i made it go something like this:

<?php

print ("$name&nbsp$tag");

?>

That might work to just show the most current message but thats not what I want, plus i think it would only show your message and not others'. Any help?AH HA! I did it.
<!-- m --><a class="postlink" href="http://www.ramptstudios.com/php/tag.php">http://www.ramptstudios.com/php/tag.php</a><!-- m -->
I just learned about flat files and used an empty .txt
There is one bug though, when there are too many posts for the size of the table, it expands the table instead of creating scroll bar. How can i fix that?Put it in a div with overflow:auto;

You'll also need to use the strlen() (<!-- m --><a class="postlink" href="http://www.php.net/strlen">http://www.php.net/strlen</a><!-- m -->) function to break it after a certain number of characters if somebody enters something like
fffffffffffffffffffffffffffffffffffffffffffffffffflike put this right beofre the table:
<div align="center" overflow:auto;>
?No, make it so the comments go inside the div. like this

<div style="overflow:auto; width:100px; height:400px;">
posts here
</div>sweetness. It works. Now where do i add that horizontal break thing?something like this

if(strlen($str) >= 20){
echo 'please insert some spaces in your comment';
exit;
}
else{
//post
}


There's a way of making it insert a break after a certain number of characters, i'm just not sure how to do that.Another thing that i can't figure out is why the name is further away from the name box than the message is from its box
Name:

<tr>
<td border="0" bgcolor="#404040" width="200" align="center" valign="top">
<font color="#FFFFFF">Message:</font>
<input type="text" name="tag">
</td>
</tr>

and message:

<tr>
<td bgcolor="#808080" width="200" height="15" align="center" valign="top">
<input type="submit" name="submit" value="Post Comment">
</form>
</td>

Plus how do I cange the style of the borders?They look the same to me.Theres a line between name and its enrty form for me. And yet another problem. When ever you type a ' in a message it adds a / before it. What's up with that? Like if i said:

I'm hungry
it would show I/'m hungryYou need to use the stripslashes function. Like this

stripslashes($_POST['message'])See, when you say that I don't know where you want me to put it. Here is the post php

<?php

if ($_GET[mode] == "post") {
if(empty($_POST[name])) {
echo "You did not enter any name!";
}
elseif(empty($_POST[tag])) {
echo "You did not enter any message!";
}
else {
$file = fopen("flat.txt","a+");
fwrite($file,"$_POST[name]: $_POST[tag]<!--new message-->");
fclose($file);
}
}

$file = fopen("flat.txt","a+");
$contents = fread($file, filesize("flat.txt"));
fclose($file);

$messages = explode("<!--new message-->",$contents);
$messages = array_reverse($messages);

for ($i = 0; $i < count($messages); $i++) {
echo "$messages[$i]<br>\n\n";
}

?>there is a line because you have the message between the rows.What rows?
I'm sorry Ive only been doing this for a shoret while and I'm trying to learn as much as I can.<tr>
<td border="0" bgcolor="#404040" width="200" align="center" valign="top">
<font color="#FFFFFF">Message:</font>
<input type="text" name="tag">
</td>
</tr>

and message:

<tr>
<td bgcolor="#808080" width="200" height="15" align="center" valign="top">
<input type="submit" name="submit" value="Post Comment">
</form>
</td>
the bolded are table rows, you cannot have anything between the rows in a table.I see what you are talking about. It is a lot further now.
<!-- m --><a class="postlink" href="hppt://www.ramptstudios.com/uber">hppt://www.ramptstudios.com/uber</a><!-- m -->
It only needs one more thing: email/url entry form Should be easy 'nuff.Oh and dont post anything testing if you can destroy the page with html, php, ect. All of it is blocked.I cant figure out how to have a url/email box. I have the email one know but I don't know how to have it check whether to use the <a href=http://www.htmlforums.com/archive/index.php/"mailto:blablah@blah,com">Name</a>
or the regualr url tag
<!-- m --><a class="postlink" href="http://ramptstudios.com/uber/">http://ramptstudios.com/uber/</a><!-- m -->
help.Originally posted by GuitarHero
I cant figure out how to have a url/email box. I have the email one know but I don't know how to have it check whether to use the <a href=http://www.htmlforums.com/archive/index.php/"mailto:blablah@blah,com">Name</a>
or the regualr url tag
<!-- m --><a class="postlink" href="http://ramptstudios.com/uber/">http://ramptstudios.com/uber/</a><!-- m -->
help.
ok, that didn't make any sense. what is a email box? what are you trying to do?
 
Back
Top