Click to See Complete Forum and Search --> : Flat File Chat


Anon
07-11-2001, 07:06 AM
Hello
I have created a flat file chat, but I have some troubles to solute before upload:
1. Every time the script updates (which it does every 8 second) the last input is repeated. How can I avoid this repeate whithout canceling my update?
2. I would like the inputs to be sorted after date - rsort($ar) or sort($ar) does not work in this case, because it sorts alfabetic and then date.
3. Is it possible to have a limit of lines in the text file? I have tried to set 100 as limit in the for-loop, but then I have at lot of -> and empty space in the bottom of the textarea. Moreover there can still be far more than 100 lines in the textfile.

Greetings from Jan

My script follows here:

Function ShowText(){
$ar=file("textfile.txt");
for($i=0;$i<100;$i++){
list($data[date],$data[name],$data[text])=explode("|",$ar[$i]);
rsort($ar);
print "$data[date]"."&nbsp;&nbsp;"."$data[name]"."&nbsp;->&nbsp;&nbsp;"."$data[text]";
}
}

Function WriteData(){
global $name,$text;
if ($action="submitdata"){
print "<form action=\"$PHP_SELF\" method=GET>";
print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"submitdata\">\n";
print "Name: ";
print "<input type=text name=name value='$name' size=10 maxlength=10>";
escapeshellcmd($name);
print "&nbsp;&nbsp;&nbsp;";
print "Text: ";
print "<input type=text name=text size=40 maxlength=160>";
escapeshellcmd($text);
print "&nbsp;&nbsp;&nbsp;";
print "<input type=submit value=\"Submit\">";
print "&nbsp;&nbsp;&nbsp;";
print "<input type=reset value=\"Reset\">";
print "</form>";
$dato=date("H:i:s");
$line="$date|$name|$text\n";
$fp=fopen("textfile.txt","a");
fwrite($fp,$line);
fclose ($fp);
}
}

print "<TABLE cellspacing=0 cellpadding=1 border=0>";
print "<TR><TD width=\"570\" align=left valign=top>";
print "<TEXTAREA name=tekst cols=69 rows=14>";
ShowText();
print "</TEXTAREA>";
print "</TD></TR>";
print "<TR><TD>";
print "<BR>";
print "</TD></TR>";
print "<TR><TD>";
WriteData();
print "</TD></TR>";
print "</TABLE>";

Anon
07-30-2001, 01:58 PM
after you do the update, redirect to the 'view' script instead of just displaying the results in the update script. (i didn't read your code, but you should set it up in that fashion)

then, you would always be refreshing the view script only.

sablos
08-20-2001, 11:15 AM
one solution for limiting the number of messages in your chat is to use fopen("textfile.txt", "w+") instead of appendding the data, you'd overwrite the file everytime. You'd also have to parse and recreate the array on every run. This would be a little more resource taxing, but you'd save disk space in the long run from an ever-growing textfile.txt.

Good luck if your chat works!