Share some common methods of PHP implementation to the text database
PHP can realize the data of the text database display, add, modify, delete, query and so on five basic operations.
We take a message this program as an example, the PHP implementation of the text database data display, add, modify, delete, query five basic methods of operation.
There are 10 fields in this text database: Customer IP, speaking time, customer name, customer email, customer homepage address, message expression image name, customer QQ, customer image image, message content, administrator reply content.
1. Join the Data program:
$date =date ("y-m-d h:i:s");//acquisition system time $IP = $HTTP _server_vars[remote_addr]; IP address $text=encode ($GB _text);//Remove the space after the message. $FP =fopen ("Gb.dat", "a");//Open the Gb.dat text file in write-only mode with the file pointer pointing to the end of the file. $str =$ IP. "|". $date. "|". $GB _name. "|". $GB _email. "|". $GB _home. "|". $face. "|". $GB _qq. "|". $head. "|". $text. "|". $reply. " ";//Assign the data of all the messages to the variable $str," | " The purpose of this is to use the data interval symbol for future data segmentation. Fwrite ($fp, $STR);//write data to File fclose ($FP);//Close File ShowMessage ("Message succeeded!", "index.php", "3");//Message successful, 3 seconds after the automatic return to the main interface.
Among them, $gb_name, $GB _email, $GB _home, $face, $GB _qq, $head, $GB _text, $reply are data from the statement only son.
2. Display Data program:
0) {//If the number of messages is greater than 0 (that is, the text database is not empty) $total =ceil ($num/$pagesize);//Calculates the total number of pages (the largest integer, that is, where the decimal point is rounded up, $pagesize the amount of information displayed per page for a preset) if ($page <1) {//If the current page number is less than 1$page=1;//, assign a value of 1} $number = ($page-1) * $pagesize;//Calculate the digital number of the first message currently displayed (digital zero-based, Mainly to achieve the purpose of corresponding to the array unit number) for ($i =0; $i <= $pagesize-1; $i + +) {//Enter the Loop $row=explode ("|", $arr [$number]);//with "|" As a separator, divide the data for each of the cells 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 cell data in the array $row sequentially to the variable in parentheses?> >//display the image of the customer
Nickname
】
Show client name Posted in:
Show message release time
>//Display Customer Message emoticons image
Said:
Show customer message Content
Show reply content
"Rel=" external nofollow "target=" _blank "> Access
Home//Customer homepage for hyperlinks "rel=" external nofollow "> to
Send//Customer e-mail connection
The QQ number is
Show customer's QQ number
The IP address is
"//Display the client's IP address www.jbxue.com" rel= "external nofollow" > reply/Message reply to the connection statement "rel=" external nofollow "> Delete// Message DELETE statement (with customer message time $datetime as the deletion of identity)
1)//If the current page digital number is not equal to 1{$back = $page -1;//The current page digital number minus 1, and assign this value to the variable $backecho "first page";//Display the first page of the connection echo "Previous page"; The current page digital number equals $back and shows its connection} if ($page <> $total)//If the current page digital number is not equal to the total number of pages (the last digital number) {$next = $page + 1;//The current page digital number plus 1 and assign the variable $nextecho "next page";//Display next page connect echo " Last Page "; Show last page connection} echo "pages: $page/$total";//Display the current page digital number and display the last page digital echo "A total of $num message";//Display message}else {echo "
There are no messages at this moment!
";//information displayed if the contents of the file are empty}else {echo"
Data file is missing, please contact the administrator! or post a message to re-establish the data file!
";//If the file does not have the displayed information}
3. Data modification Procedure:
$list =file ("Gb.dat");//Read the entire Gb.dat file to the array $list, each element of the array is a message ($list [0] is the first message data, $list [1] is the first message data .... $n =count ($ list);//Calculate the total number of messages in the $list content and assign the variable $nif ($n >0) {//If the number of messages is greater than 0$fp=fopen ("Gb.dat", "w");//The file is opened in write-only mode gb.dat$gb_reply= Encode ($GB _reply), for ($i =0; $i < $n; $i + +) {//Enter loop if (eregi ($ttime, $list [$i])) {//will send message time $ttime with array unit $ The list contents are matched by string matching $f=explode ("|", $list [$i]);//If a match is found, the "|" As a delimiter, open the message $list[$i] ($i), and assign the data to the array $f$f[9]= $GB _reply;//$f[9] (message last data) with $gb_reply (reply content) instead. $list [$i]= $f [0]. "|". $f [1]. "|". $f [2]. "|". $f [3]. "|". $f [4]. "|". $f [5]. "|". $f [6]. "|". $f [7]. "|". $f [8]. "|". $f [9]. " "; $list[the contents of an array cell to $i] with an array of $f with the delimiter "|" instead (where $f[9] is the new data that was modified). break;//jump out loop}}//loop Terminator}for ($i =0; $i <= $n; $i + +) {//Enter loop fwrite ($fp, $list [$i]);//write file $list for each cell of the array gb.dat}// Loop Terminator fclose ($FP);//Close File ShowMessage ("Reply succeeded!", "index.php");//reply successful, automatically return to the main interface.
4. Data Removal Program:
$list =file ("Gb.dat");//Read the entire Gb.dat file to the array $list, each element of the array is a message ($list [0] is the first message data, $list [1] is the first message data .... $n =count ($ list);//Calculate the total number of messages in the $list content and assign the variable $nif ($n >0) {//If the number of messages is greater than 0$fp=fopen ("Gb.dat", "w"),//The file is opened in write-only mode gb.datfor ($i =0; $i < $n, $i + +) {//Enter the loop if (eregi ($ttime, $list [$i])) {//will be sent to send message time $ttime and array $list[$i] in the string matching comparison $list [$i]= "";//If the match succeeds, the $ list[$i] Empty (for deletion purposes) break;//jump out of the loop}}//Loop Terminator for ($i =0; $i <= $n; $i + +) {//Enter loop fwrite ($fp, $list [$i]);//will array $ The list of each cell is a row, written to the file Gb.dat}//Loop terminator fclose ($FP);//Close File ShowMessage ("Delete succeeded!", "index.php");//delete successful, automatically return to the main interface. }
5, data query procedures:
Above is the search form statement segment
you did not enter any keywords! ";//Display}else {if ($n >0) {//If the number of messages is greater than 0for ($i =0; $i < $n; $i + +) {//Enter the loop if (eregi ($found, $list [$i])) {//Enter the keyword with an array of $ list[$i] The string to match the comparison $row=explode ("|", $list [$i]); $id = 1; If a match is found, the "|" As a delimiter, open the message $list[$i] (section $i), and assign the data to the array $row. and assign the variable $id to 1 so as to determine whether a match is found. List ($ip, $datetime, $name, $email, $home, $face, $qq, $head, $text, $reply) = $row;//?> the cell data in the array $row in order to assign the variable in parentheses >// Display images of customer image
Nickname "
"
Show client name Posted in:
//Show Message time
>//Display Customer message emoticon image
said:
//Show Customer
http://www.bkjia.com/PHPjc/822277.html www.bkjia.com true http://www.bkjia.com/PHPjc/822277.html techarticle share some common methods of PHP implementation of the text database PHP can realize the text database data display, add, modify, delete, query and other five basic operations. We have a ...