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

Source: Internet
Author: User

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, 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, "|", 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
<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 = "<? _ Fcksavedurl = "" <? "Echo $ home?> "Target =" _ blank "> access <? Echo $ name?> Homepage </a> // hyperconnections of the customer Homepage
<A href = "mailto: <? Echo $ email?> "> To <? Echo $ name?> Send a mail </a> // connect the customer's E-MAIL to the network management ujia www.bitscn.net
<? 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? Time = <? Echo $ datetime?> "> Reply </a> // connection statement for message reply
<A href = "del. php? 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? Page = 1> first page </a> "; // display the connection on the first page
Echo "<a href = index. php? 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? Page = $ next> next page </a> "; // displays the next page connection.
Echo "<a href = index. php? 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
}
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]. ""; // 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
<Form action = "search. php" method = "post">
<Font color = "# 0099CC" size = "2"> search keywords:
<Input name = "found" type = "text" id = "found" style = "background-color: # FFFFFF; color: # 8888AA; border: 1 double # 3399CC "size =" 12 ">
<Input name = "submit" type = "image" src = "image/search.gif" alt = "message search">
</Font> </td>
</Tr>
</Table>
</F

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.