Home > Article > Backend Development > Implementation and techniques of dynamic news publishing_PHP tutorial
To build a website of a certain scale, dynamic news releases are essential. There are many ways to implement it. It is recommended to use text files to generate it, which is fast, simple and trouble-free. Well, let's get to work right away.
First, we assume that there is already a folder named "news" under "c://news", which is used to store the text of news. And we assume that the names of these texts are the titles of the news to be published.
1. First, we restrict reading the pointer of the folder.
$handle=dir("c://news");
2. Use a while statement to get the pointers of each text file and output them one by one.
while($file=$handle->read())
{
echo $file;
}
3. After completing the operation of 2, observe the output of the results from IIS , and found that in addition to listing the names of all text files, there will be two more "strange symbols" on the page.
.
. .
The origin of these two logos is not the scope of our discussion today, but their appearance will affect the "press release" of our web page, so it is recommended to use an if statement to skip them when displaying.
4, use chop() to remove the ".txt" after the file name
$filename=chop($file,".");
In this way, $filename[0] is the news we require title.
5. After completing the display, you need to make a link. We assume that the file that processes and displays news is show.php;
To summarize the above, we can write the program like this
$handle=dir("c://news");
while ($file=$handle->read())
{
if(($file!='.')&&($file!='..'))
{
$filename=chop($file,".");
echo " filename[0] ";
}
?>
The next step is to output text on the web page. There are already many instructions in this regard. I won't repeat it again.