PHP automatically updates news DIY_PHP tutorial-php Tutorial

Source: Internet
Author: User
Tags filetime
PHP automatically updates news DIY. 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, when we browse some websites, we often see news that has just been updated. manual maintenance is a very tedious task. However, we can use programs to control the update operations, which makes it 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> <HEAD> <TITLE> News TITLE </TITLE> </HEAD>" (for further reasons ).

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 ++) // sort by the bubble algorithm. (I have already introduced this article in the computer newspaper. I am not so embarrassed)
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.

Bytes. However, we can...

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.