PHP automatically updates news DIY

Source: Internet
Author: User
Tags filetime

When we browsed some websites, we often saw news that was just updated. If we had manual maintenance, it would be a very tedious task. However, we can Program To control the update operation, it will become very convenient (I am not going to be lazy ).

Here, I use PHP to implement this function. The principle is to sort the news in reverse order based on the news release time, and display the News Connection list on a page. Each connection corresponds to a news content webpage. The procedure is as follows:

1. First, create a directory under your site and save the edited news pages under this directory. In the future, all news pages will be placed here for easy maintenance. News pages are in hypertext format (not to mention html ), the first behavior is "<HTML>

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

The source program is as follows:

<? PHP
$ Fp = array ("FILENAME" => "", "filetime" => "", "firstline" => ""); // create an array to save the file name and the first line of the file
$ Dd = Dir ('Save the news directory'); // read the Save directory of the news File
$ I = 0;
Clearstatcache ();
While ($ file = $ DD-> Read () // read the files in the directory cyclically
{
If (is_file ($ DD-> path. "/". $ file ))
{
$ FP [$ I] ["FILENAME"] = $ DD-> path. "/". $ file; // save the 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 do we need to write the news header line in the required format)
Fclose ($ fr );
If ($ time = date ("y m d H: I", filemtime ($ DD-> path. "/". $ file) // save the file time as the sorting condition.
{
$ FP [$ I] ["filetime"] = $ time;
}
$ I ++;
}
}
$ I = count ($ FP); // number of files saved
$ I-= 4;
For ($ J = 0; $ j <$ I; $ J ++) // follow the bubble Algorithm Sort (Computer reports already exist Article Introduced, I am not so arrogant)
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 <= (count ($ FP)-4); $ I ++) // read the saved file information and connect it accordingly
{
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, and then input http: // website name/directory/panxu. php In the browser. Can you see it?

Finally, let's say a few more words (I am not making more money, but mainly serving the people). Your website must support the PHP function.

In addition, I only provide basic functions. You can add more control functions, as well as page modification and beautification. What do you need? Do you mean I am not responsible? I am suffering ...). Well, it will be here first. What is wrong, please give me more advice, can send email: zbclh@sina.com and I contact.

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.