PHP displays, adds, modifies, deletes, and queries data in a text database.

Source: Internet
Author: User
PHP implements five basic operations on the Text Database: data display, addition, modification, deletion, and query. I use a message book program as an example, describes how PHP 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 "> <LINKhref =" http: // www. php100

PHP implements five basic operations on text databases: data display, addition, modification, deletion, and query.
I used a message book program as an example to illustrate how PHP 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. "\ n"; // assign all message data to the variable $ str, "|", which 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 "," 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 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


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


// Display the reply content


"Target =" _ blank "> access Homepage // hyperconnections of the customer homepage
"> Send mail // customer E-MAIL connection network administrator U Home www.bitscn.net
The QQ number is // Display the customer's QQ number
The IP address of IS "// Display the customer's IP address
"> Reply // connection statement for message Reply
"> Delete // the message deletion statement (with the customer message time $ datetime as the deletion Identifier)


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 "Page 1"; // display the connection on page 1
Echo "previous page"; 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 "next page"; // Display next page connections
Echo "last page"; display 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"

There are no messages currently! "; // Information displayed if the file content is empty
}
Else {
Echo" Data File loss. please contact the administrator! Or publish a message to recreate the data file! "; // Display information if the file does not exist
}
3. data modification section
$ List = file ("gb. dat "); // read the entire gb. the dat file goes to the array $ list. each element of the array is a message ($ list [0] is the data of the first message, and $ list [1] is the data of the first message ..... www.bitscn.net
$ N = count ($ list); // calculates the total number of messages in the $ list content and assigns the variable $ n
If ($ n> 0) {// if the number of messages is greater than 0
$ Fp = fopen ("gb. dat", "w"); // open the file gb. dat in write-only mode.
$ Gb_reply = encode ($ gb_reply );
For ($ I = 0; $ I <$ n; $ I ++) {// enters the loop
If (eregi ($ ttime, $ list [$ I]) {// match the sent message time $ ttime with the content in the array unit $ list
$ F = explode ("|", $ list [$ I]); // if a match is found, use "|" as the separator, cut down the message $ list [$ I] (Message $ I) and assign the data to the array $ f
$ F [9] = $ gb_reply; // replace $ f [9] with $ gb_reply (Reply content.
$ List [$ I] = $ f [0]. "| ". $ f [1]. "| ". $ f [2]. "| ". $ f [3]. "| ". $ f [4]. "| ". $ f [5]. "| ". $ f [6]. "| ". $ f [7]. "| ". $ f [8]. "| ". $ f [9]. "\ n "; // replace the content of the array unit $ list [$ I] with the separator "|" in array $ f ($ f [9] is the new data modified ).
Break; // skip loop
}
} // Loop Terminator
}
FOR ($ I = 0; $ I <= $ n; $ I ++) {// enters the loop
Fwrite ($ fp, $ list [$ I]); // write each cell in the array $ list into a row and into the file gb. dat
} // Loop Terminator
Fclose ($ fp); // close the file
Showmessage ("reply successful! "," Index. php "); // if the response is successful, the system automatically returns to the main interface.
4. data deletion section
$ List = file ("gb. dat "); // read the entire gb. the dat file goes to the array $ list. each element of the array is a message ($ list [0] is the data of the first message, and $ list [1] is the data of the first message .....
$ N = count ($ list); // calculates the total number of messages in the $ list content and assigns the variable $ n
If ($ n> 0) {// if the number of messages is greater than 0
$ Fp = fopen ("gb. dat", "w"); // open the file gb. dat in write-only mode.
For ($ I = 0; $ I <$ n; $ I ++) {// enters the loop
If (eregi ($ ttime, $ list [$ I]) {// match the sent message time $ ttime with the strings in the array $ list [$ I]
$ List [$ I] = ""; // if the match is successful, the $ list [$ I] is cleared (for deletion)
Break; // skip loop
}
} // Loop Terminator
FOR ($ I = 0; $ I <= $ n; $ I ++) {// enters the loop
Fwrite ($ fp, $ list [$ I]); // write each cell in the array $ list into a row and into the file gb. dat
} // Loop Terminator
Fclose ($ fp); // close the file
Showmessage ("deleted successfully! "," Index. php "); // if the deletion is successful, the system automatically returns to the main interface.

}

5. Data Query section

//// // The above is the search form statement segment
$ Id = 0;
$ List = file ("gb. dat "); // read the entire gb. the dat file goes to the array $ list. each element of the array is a message ($ list [0] is the data of the first message, and $ list [1] is the data of the first message .....
$ N = count ($ list); // calculates the total number of messages in the $ list content and assigns the variable $ n
$ Found = trim ($ found );
If (! $ Found) {// if $ found is false
Echo" You have not entered any keywords! "; // Relevant display


}
Else {
If ($ n> 0) {// if the number of messages is greater than 0
For ($ I = 0; $ I <$ n; $ I ++) {// enters the loop
If (eregi ($ found, $ list [$ I]) {// The input keyword matches the string in the array $ list [$ I ].
$ Row = explode ("|", $ list [$ I]); $ id = 1; // if a match is found, use "|" as the separator, cut down the message $ list [$ I] (Message $ I) and assign the data to the array $ row. and assign the variable $ id to 1 to determine whether matching is found.
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


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


// Display the reply content
"Target =" _ blank "> access Homepage // hyperconnections of the customer homepage
"> Mail/client E-MAIL connection
The QQ number is // Display the customer's QQ number
The IP address of IS "// Display the customer's IP address
"> Reply // connection statement for message Reply
"> Delete // the message deletion statement (with the customer message time $ datetime as the deletion Identifier)


}
} // Loop Terminator
}}
If ($ id = 0) {echo" No matching message found! ";} // If $ id = 0, no matching is found and a prompt is displayed.


?>

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.