PHP implements five basic control methods for data display, participation, correction, deletion, and query in a text database. I use a message book program as an example, discuss the five foundations of PHP for data display, participation, correction, deletion, and query of text databases
PHP implements five basic control methods for text database data display, participation, correction, deletion, and query
I will use a message book program as an example to discuss the five basic control methods of PHP to display, participate, modify, delete, and query data in a text database.
This text database has 10 fields: customer IP address, speech time, customer name, customer EMAIL, customer homepage address, message emoticons image name, customer QQ, customer Image image, message content, administrator reply content.
1. participate in the data program section.
$ Date = date ('Y-m-d H: I: s'); // Obtain the system time
$ Ip = $ HTTP_SERVER_VARS [REMOTE_ADDR]; // obtain the ip address of the speaker.
$ Text = encode ($ gb_text); // drop the space behind the message content.
$ Fp = fopen ('GB. dat ', 'A'); // open the gb. dat text file in write-only mode. the file pointer points to the end of the file.
$ Str = $ ip. '| '. $ date. '| '. $ gb_name. '| '. $ gb_email. '| '. $ gb_home. '| '. $ face. '| '. $ gb_qq. '| '. $ head. '| '. $ text. '| '. $ reply. '\ n'; // assign the data of all messages to the variable $ str,' |.
Fwrite ($ fp, $ str); // write data into a file
Fclose ($ fp); // close the file
Showmessage ('message successful! ', 'Index. php', '3'); // The message is sent successfully. The message is returned to the main interface three seconds later.
$ Gb_name, $ gb_email, $ gb_home, $ face, $ gb_qq, $ head, $ gb_text, and $ reply are data transmitted from the form.
2. data display section
If (file_exists ('GB. dat ') {// check whether the file exists
$ Array = file ('GB. dat '); // read all the file content into the array $ array
$ Arr = array_reverse ($ array); // arrange the data in $ array in a reverse manner (that is, the last row is the first row, and so on) read every unit in the array $ arr ($ arr [0]...).
$ Num = count ($ array); // get the number of information in the array $ array (one row moves one message)
If ($ num> 0) {// if the number of information is greater than zero (that is, the text database is not empty)
$ Total = ceil ($ num/$ pagesize); // calculates the total number of pages (take the maximum integer, that is, any number of decimal points is rounded to an integer, $ pagesize indicates the number of information displayed on each page)
If ($ page <1) {// if the current page digital number is less than 1
$ Page = 1; // The value is 1.
}
$ Number = ($ page-1) * $ pagesize; // calculate the number of the first message displayed currently (the number starts from scratch, it is important to achieve the target corresponding to the array unit number)
For ($ I = 0; $ I <= $ pagesize-1; $ I) {// feed loop
$ Row = explode ('|', $ arr [$ number]); // use '|' as the delimiter to split the data of every $ number unit in the array $ arr, and assign the data to the array $ rom
List ($ ip, $ datetime, $ name, $ email, $ home, $ face, $ qq, $ head, $ text, $ reply) = $ row; // assign the unit data in the array $ row to the variable in the brackets in order
?>
> // Display the customer Image
Nickname 【 ]
// Display the customer name
Posted on: // Display the message posting time
> // Display the customer's message and emoticons
Said: // Display the customer's message content