PHP Auto Update news DIY

Source: Internet
Author: User
Tags format count filetime functions html tags save file
When we browse some websites, we often see some news is just updated, if it is a manual maintenance, it will 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 have HTML), the first behavior "<HTML> <HEAD> <TITLE> News title </TITLE> </HEAD>" (cause explained later).

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 news file's save directory
$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);//Remove HTML tags and save the first line of the file (which 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 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 (computer has an article introduced, I don't wordy)
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, 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> \ n ";
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?

Finally, a few more words (I do not want to earn more money, mainly to serve the serving), your site must support PHP features.

And, I just provide the basic functions, you can add more control functions, as well as page decoration and landscaping work, it depends on yourself (what?). That I'm not responsible for the end? I am suffering ...). All right, let's just stop here. There is anything wrong, but also please give more advice, can send email:zbclh@sina.com and I contact Exchange.


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.