PHP basic operations on text databases. Php (as the mainstream development language) to display, add, modify, delete, and query data in a text database, I use a message-based program as an example php (as the mainstream development language) methods to display, add, modify, delete, and query data in a text database
I used a message book program as an example to illustrate how php (as the mainstream development language) can display, add, 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. add the data program segment.
$ 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); // Remove the space after 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. ""; // assign all message data to the variable $ str, "" is used as the data interval symbol for data segmentation in the future.
Fwrite ($ fp, $ str); // write data to a file
Fclose ($ fp); // close the file
Showmessage ("message successful! "," Index. php (as the current mainstream development language) "," 3 "); // The message is sent successfully. it is automatically 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 of the array $ arr ($ arr [0]...).
$ Num = count ($ array); // gets the number of information in the array $ array (one row is an item)
If ($ num> 0) {// if the number of messages 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, this mainly aims to correspond to the array unit number)
For ($ I = 0; $ I <= $ pagesize-1; $ I ++) {// enter the 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 variables in the brackets in sequence.
?>
> // Display the customer Image
<Br>
<Font color = "# 0099CC"> nickname [<? Echo $ name?> <Font size = "2">] <br> // display the customer name
Posted on: <? Echo $ datetime?> // Display the message posting time
<Br>
// Display the customer's message and emoticons
<? Echo $ name?> Said: <? Echo $ text;?> // Display the customer's message content
<Br>
<? Echo $ reply?> // Display the reply content
<Br>
<A href = "<? Echo $ home?> "Target =" _ blank "> access <? Echo $ name?> HomePage </a> // hyperconnections of the customer homepage
<A href = "mailto: <? Echo $ email?> "> To <? Echo $ name?> Mail </a> // connection to customer E-MAIL
<? Echo $ name?> The QQ number is <? Echo $ qq?> // Display the customer's QQ number
<? Echo $ name?> The IP address of is <? Echo $ ip?> "// Display the customer's IP address
<A href = "reply. php (as the mainstream development language )? Time = <? Echo $ datetime?> "> Reply </a> // connection statement for message Reply
<A href = "del. php (as the mainstream development language )? Time = <? Echo $ datetime?> "> Delete </a> // the message deletion statement (with the customer message time $ datetime as the deletion Identifier)
<Br>
<?
If ($ number = $ num-1) // if the unit number of the array is equal to the total number of messages minus one (because the unit number starts from zero, so this means this is the last message)
{
Break; // skip loop
}
$ Number = $ number + 1; // array unit number plus 1
} // Loop Terminator
}
If ($ page <> 1) // if the current page digital number is not equal to 1
{
$ Back = $ page-1; // subtract 1 from the current page number and assign this value to the variable $ back
Echo "<a href = index. php (as the mainstream development language )? Page = 1> first page </a> "; // display the connection on the first page
Echo "<a href = index. php (as the mainstream development language )? Page = $ back> Previous page </a> "; the current page digital number is equal to $ back and its connection is displayed
}
If ($ page <> $ total) // if the current page digital number is not equal to the total page number (the last page number)
{
$ Next = $ page + 1; // add the digital number of the current page to 1 and assign the variable $ next
Echo "<a href = index. php (as the mainstream development language )? Page = $ next> next page </a> "; // displays the next page connection.
Echo "<a href = index. php (as the mainstream development language )? Page = $ total> last page </a> "; displays the last page connection
}
Echo "page: $ page/$ total"; // display the current page number and the last page number
Echo "$ num messages in total"; // displays the number of messages
}
Else {
Echo "<center> there are no messages currently! </Center> "; // information displayed if the file content is empty
}
Else {
Echo "<center> Data File loss. contact the administrator! Or publish a message to recreate the data file! </Center> "; // if the file does not exist
}
Http://www.bkjia.com/PHPjc/508651.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508651.htmlTechArticlephp (as the mainstream development language) to achieve the text database data display, add, modify, delete, query five basic operations method I use a message program as an example...