PHP Auto Update news DIY

Source: Internet
Author: User
Tags filetime html tags save file

We browse some websites, often see some news is just updated soon, if it is artificial maintenance, it would be a very cumbersome work. However, we can use the program to control the implementation of the update operation, things will become very convenient (I am not lazy).

Here, I use PHP to implement this feature. The principle is based on the press release time in reverse order, and then on a page to display a list of news connections, each connection corresponds to a news content page. The specific actions are as follows:

First, in your site to create a directory, the edited news pages are saved in this directory, all future news pages are placed here, convenient maintenance. The News page format is hypertext (not to mention you will not be able to HTML), the first behavior " News title " (cause then explain, must be in the same line!) )。

Second, write the program to realize the News Automatic Update function (assuming file name is paixu.php)

The source program is as follows:

<?php
$fp=array ("filename" => "", "Filetime" => "", "Firstline" => "");//create array, save file name, file first line
$dd=dir (' News Save Directory ')//Read the Save Directory of news files
$i=0;
Clearstatcache ();
while ($file=$dd->read ())//loop read out the files in the directory
{
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)//To save the first line of the file after removing the HTML tags (that is, why we want to write the first line of the news page in the required format)
Fclose ($FR);
if ($time=date ("Y m D h:i", Filemtime ($dd->path.) /". $file))//Save the file time as a sort condition
{
$fp[$i]["FILETIME"]=$time;
}
$i;
}
}
$i=count ($FP)//number of files saved
$i-=4;
for ($J=0;$J<$I;$J)//sorted by bubble algorithm
for ($k=$i;$k>$j;$k--)
if ($fp[$j]["filetime"]<=$fp[$k]["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<= ($FP)-4); $i)//Read the saved file information, do the appropriate connection
{
echo "<tr> <td>";
echo "<a Href=". $fp[$i]["filename". " > ". $fp[$i][" Firstline "]." </a> ";
echo "</td> <td class=font1>";
echo "(". $fp[$i]["Filetime"]. ") <br> ";
echo "</td> </tr>";
}
$dd->close ();
? >

Third, put the program and directory on your site, and then in the browser typed http://site name/directory/panxu.php, can you see?

This procedure only provides the basic function, you may add more control function, but also has the page adornment and the beautification and so on work, depends on yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.