Author: Chen Xinzheng Source: Tianji.com
Preface: Nowadays, many friends who have personal homepages or blogs choose to rent virtual hosts, and a considerable number of virtual hosts support PHP. If you have a hosting space that supports PHP, have you armed your website with PHP?
Can you let PHP add some content to your web page that automatically updates every once in a while to better attract visitors? Implementing this function does not require advanced programming knowledge. Please take a look at the following example:
(1) English proverb
Let us look at a simple application: let the PHP program randomly extract a row of records from the database and display it on the web page, every 15 seconds minutes to read another record. Proverbs in English and Chinese are suitable content for display.
Considering that most cheap virtual hosts hosting personal homepages do not support databases such as MySQL, we use text data files to store data, which is enough for websites with low traffic. Create a text file named english.dat with the following content:
A bad beginning makes a bad ending. He who hates the beginning will hate the end.
A bad bush is better than the open field. Something is better than nothing.
A bad compromise is better than a good lawsuit. A bad compromise is better than a good lawsuit.
A bad conscience is a snake in one's heart. A bad conscience is a snake in one's heart.
A bad custom is like a good cake, better broken than kept. A bad custom is like a good cake, better broken than kept.
A bad padlock invites a picklock.
...
Note that each line is a complete record, and there should be no line breaks in the middle; there cannot be half-width double quotes ( " ) in the file, otherwise the program will make parsing errors when outputting data. There are a lot of such content on the Internet, just search it, or You can go to my website http://www.musicfly.net/english.dat to download a data file.
The following is a program to read the 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); //Load the data file into an array
$num_lines = count($data); //The number of data file lines, that is, the number of records 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 cut off the last newline character
echo "document.write("$content");"; //Use javascript Format output data
?>
Save english.dat and english.php and upload them to the same directory on the server.
The last step is to let your web page call this php program to display the content. Because the result of php operation is output in the form of javascript, we only need to call this php as a javascrupt. The method is: use notepad or web page editing software to open the html file you want to modify, and add the dynamic content to the html file you want to modify. Add the following code to the displayed position:
<script language="javascript" src="english.php"></script>
Note that if the html file you modify is not in the same directory as english.php, you must put it in src=".. ." indicate the relative path of english.php.
Upload this 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 and see if the displayed content has changed? And you don't have to do any extra work to update these contents, the php program does it for you.
If your friend wants to implement the same function on his own web page or blog but does not have a host that supports PHP, he can use <script language="javascript" src="http://your website/program path/english. php"></script> method to call the program on your host to display the same content.
Of course, this program may also have the problem of incompatibility of encoding methods, because the content is displayed in gb2312 encoding. When used on UTF-encoded web pages, the Chinese will become garbled. The solution is beyond the scope of this article, please search for relevant tutorials.
After realizing this function, you have begun to move from the static world of html to the dynamic world of php. The days are long, next time, let’s take a look at how to use php to add a “daily horoscope” function to your web page.
(The programs in this article are all run in the php4/php5 environment.)