PHP automatic update news DIY_PHP tutorial

WBOY
Release: 2016-07-21 16:06:39
Original
873 people have browsed it

When we browse some websites, we often see some news that has just been updated. If it is maintained manually, it will be a very tedious task. However, we can control the update operation through the program, and things will become very convenient (I am not trying to be lazy).

Here, I use PHP to implement this function. The principle is to arrange the news in reverse order according to the time of release, and then display a list of news links on one page, with each link corresponding to a news content web page. The specific operations are as follows:

1. First, create a directory under your site and save the edited news pages in this directory. All future news pages will be placed here for easy maintenance. The format of the news page is hypertext (don't say you don't know HTML), and the header line is "

News Title" (the reason will be explained later).

2. Write a program to realize the automatic news update function (assuming the file name is paixu.php)

The source program is as follows:

<?php
$fp= array("filename"=>"","filetime"=>"","firstline"=>"");//Create an array and save the file name and first line of the file
$dd=dir('Save news Directory');//Read the directory where news files are saved
$i=0;
clearstatcache();
while ($file=$dd->read())//Loop to read the directory File in
{
if(is_file($dd->path."/".$file))
{
$fp[$i]["filename"]=$dd- >path."/".$file;//Save file name
$fr=fopen($dd->path."/".$file,"r");
$fp[$i] ["firstline"]=fgetss($fr,60);//Remove the HTML tag and save the first line of the file (that is why we have to write the first line of the news page into the required format)
fclose($fr);
if($time=date("Y m d H:i",filemtime($dd->path."/".$file)))//Save the file time as a sorting condition
{
$fp[$i]["filetime"]=$time;
}
$i++;
}
}
$i=count($fp);//Save Number of files
$i-=4;
for($j=0;$jfor($k=$i;$k>$j;$k--)
if($fp[$j]["filetime"]{
$c=$fp[$j]["filetime"];
$fname=$fp[$j]["filename"];
$fcontent= $fp["$j"]["firstline"];
$fp[$j]["filetime"]=$fp[$k]["filetime"];
$fp[$j] ["filename"]=$fp[$k]["filename"];
$fp[$j]["firstline"]=$fp[$k]["firstline"];
$fp [$k]["filetime"]=$c;//line30
$fp[$k]["filename"]=$fname;
$fp[$k]["firstline"]=$ fcontent;
}
for($i=0;$i{
echo "<tr><td>";
echo "<a href=".$fp[$i]["filename"].">".$fp[$i]["firstline" ]."</a>";
echo "</td><td class=font1>";
echo "(".$fp[$i]["filetime"].")<br >n";
echo"</td></tr>";
}
$dd->close();
?>


3. Put the program and directory on your website, then type HTTP://websitename/directory/panxu.php in the browser. Can you see it?

Finally, a few more words (I am not trying to make more money, the main thing is to serve the people), your website must support the PHP function.

Moreover, I only provide basic functions. You can add more control functions, as well as page modification and beautification, etc. It is up to you (what? You say I am not responsible? I suffer...). Okay, that's it for now. If there is anything wrong, please give me some advice. You can contact me via email: zbclh@sina.com.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315349.htmlTechArticleWhen we browse some websites, we often see some news that has just been updated. If it is manually maintained, That would be a very tedious job. However, we can...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!