PHP for your Web page update

Source: Internet
Author: User
Tags modify php and
Blog Friends choose to hire a virtual host, while a considerable part of the virtual host support PHP. If you have a host space that supports PHP, is there any use of PHP to arm your site?

Can you get PHP to add something to your Web page that automatically updates every once in a while to better attract visitors? This does not require much advanced programming knowledge, see the following example:

(a) English proverb

Let's take a look at a simple application: Let the PHP program randomly extract a row of records from the database, display it on a Web page, and read another record in 15 minutes. An English-Chinese proverb is an appropriate content to display.

Considering that most of the inexpensive virtual hosts that place personal homepages do not support databases such as MySQL, we use text data files to store data, which is sufficient for sites with less traffic. Create a text file named English.dat, which reads as follows:

A bad beginning makes a bad ending. The beginning of evil is the end of evil.

A bad Bush is better than the open field. is better than none.

A bad compromise is better than a good lawsuit. A compromise is better than a win.

A bad conscience are a snake in one ' s heart. Anxiety

A bad custom are like a good cake, better broken than kept. A bad habit is like a fresh pie, and it is better to eat than to keep.

A bad padlock invites a picklock. Kaimen.

......

Note that each line is a complete record, do not wrap in the middle, the file cannot have a half-width double quotation mark (") Otherwise the program will parse an error when outputting the data. There are a lot of such content on the Internet, a search can also go to my website http://www.musicfly.NET/english.dat download a data file.

Here's how to write a program that reads data. Create a text file, named English.dat, and add the source code as follows:

? Php
$filename = ' English.dat '; Data file name
$refresh = 900; Update cycle (in seconds, can be adjusted as needed)
$data = file ($filename); To load a data file into an array
$num _lines = count ($data); The number of rows in the data file, that is, the number of Mt_srand (Floor (Time ()/$refresh));
$id = Mt_rand (0, $num _lines-1); Randomly select a record number
$content = Chop ($data [$id]); Select the corresponding data according to the record number and intercept the last line break

echo "document.write" (\ "$content \"); "; Output data in JavaScript format
? >

Save English.dat and english.php and upload them to the same directory as the server.

The last step is to have your Web page invoke this PHP program to display the content. Because the result of PHP running is output in JavaScript, we just call this PHP as a section of Javascrupt, by opening the HTML file you want to modify with Notepad or web page editing software, and adding the following code where you want the dynamic content to appear:

Note that if you modify the HTML file and english.php are not in a directory, you should indicate the relative path of english.php in Src= "...".

Upload the modified HTML file to the server and open it in the browser. If you do every step correctly, you should be able to see the contents of the data file displayed! Refresh the page after 15 minutes to see if the contents of the display change? And you didn't do any extra work to update the content, and the PHP program did it for you.

If your friends want to implement the same functionality on their own web pages or blogs and don't have a host that supports PHP, you can http://your URL/program path/english.php by

Of course, this program may also have the problem of encoding incompatible, because the content is displayed in gb2312 encoding, used in UTF encoded pages, Chinese will become garbled. The workaround is not covered in this article, please search the tutorial.

With this functionality, you've started moving from the static world of HTML to the dynamic world of PHP. More days, next time, let's see how you can add a "daily horoscope" feature to your Web page with PHP.

(The procedures in this article are run through PHP4/PHP5 environments.) )



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.