PHP actively updates messages DIY

Source: Internet
Author: User
Tags filetime
When we browse some websites, we often see that some messages are just Updated. if it is manually protected, it will be a very tedious task. However, we can implement update control through program control, and things will become very

When we browse some websites, we often see that some messages are just Updated. if it is manually protected, it will be a very tedious task. However, we can implement update control through program control, which makes it very convenient (I am not going to be lazy ).

Here, I use PHP to achieve this effect. The principle is to arrange the message in reverse order based on the message announcement time, and display the message connection list on a page. each connection corresponds to a message content webpage. The specific control is as follows:

1. First, create a directory under your site and keep the compiled message pages under this directory. in the future, all the message pages will be put here to facilitate protection. The message page is a hypertext method (not to mention HTML ), the first action is "<HTML> <HEAD> <TITLE> message TITLE </TITLE> </HEAD>" (The reason is explained later. it must be in the same line !).

2. write a program to enable active message update (assuming the file name is paixu. php)

The source program is as follows:

<? Php
$ Fp = array ('filename' => '', 'filetime' =>'', 'firstline' => ''); // creates an array to keep the file name and the first line of the file.
$ Dd = dir ('message retained directory'); // read the retained Directory of the message 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; // retain the file name
$ Fr = fopen ($ dd-> path. '/'. $ file, 'r ');
$ Fp [$ I] ['firstline'] = fgetss ($ fr, 60 ); // leave the first line of the file after marking the HTML (that is, why do we need to write the header line of the message as the request pattern)
Fclose ($ fr );
If ($ time = date ('Y m d H: I ', filemtime ($ dd-> path. '/'. $ file) // specifies the file retention time, which is used as the sorting condition.
{
$ Fp [$ I] ['filetime'] = $ time;
}
$ I;
}
}
$ I = count ($ fp); // number of reserved files
$ I-= 4;
For ($ j = 0; $ j <$ I; $ j) // Sort 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 <= (count ($ fp)-4); $ I) // read the retained 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 enter HTTP: // website name/directory/panxu. php in the browser. can you see it?

This program only provides the basic functions, you can increase the control function, as well as page polishing and beautification work, it is up to you.

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.