Home >Backend Development >PHP Tutorial >Using text files to create message board prompts (Part 1)_PHP tutorial
The first thing is to ensure that the text file can be read and written. Before the file is ready to be written, all we have to do is to process the user's message!
This work can be done before submitting the form. We will separate the data items obtained with specific symbols, such as ampersands. It is worth mentioning that if the submitted data also contains specific characters, then we We have to convert it into other forms, and you have your own way;
In this way, we get the data in the form:
$str=”aaaaa&bbbbb&ccccc&ddddd”;
Before writing the data to the file, we add
$str=$str.”rn”
Then execute:
$fp=fopen("txt/mytxt.txt","a");
fwrite($fp,$txt );
fclose($fp);
In this way, the data we write is at the end of the file
How to make the latest message appear at the top of the message board?
With the above processing, the data will be easy to handle,
$date=file(“txt/mytxt.txt”);
$n=count($date);
for ($ I=$n;$I>0;$I--){
$str=explode(“&”,$date[$I])
.
//The $str array that will be obtained Various outputs.
.
}
This way the latest inserted message will remain at the top.