What are the methods of working with the text database in PHP? PHP Five ways to manipulate text databases

Source: Internet
Author: User
What does PHP do with text database data? PHP implementation of the text database data display, add, modify, delete, query five basic methods of operation. So, how does PHP implement the data manipulation method of the text database, and then we'll take a look.
I'm using a message. This program as an example, the implementation of PHP to the text database 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, Add Data Program section.

$date =date ("y-m-d h:i:s");//acquisition of system time $IP = $HTTP _server_vars[remote_addr]; IP address $text =encode ($GB _text);//Remove the space following the message. $FP =fopen ("Gb.dat", "a");//Open the Gb.dat text file in write-only mode, and 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//To assign the data of all the messages to the variable $str, "" is used for future data segmentation when the data interval symbol. 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. Data Display Program section

? if (file_exists ("Gb.dat")) {//detects whether the file exists $array =file ("Gb.dat"),//reads the entire contents of the file into the array $array $arr =array_reverse ($array);//Will $ The data in the array is inverted (that is, the first line of the last row, and so on) reads into each unit of the array $arr ($arr [0] ... )。 $num =count ($array);//Gets the number of information in the array $array (a behavior one message) if ($num >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, $pagesize the amount of information displayed per page for the preset) if ($page 1) {//If the current page number is less than 1 $page =1;//is assigned a value of 1} $number = ($page-1) *$ PAGESIZE;//calculates the digital number of the first message currently displayed (digital number starting from zero, mainly to achieve the purpose 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 of 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 customer image Image <br> <font color= "#0099CC" > nickname "? echo $name? ><font size= "2" > "<br>//Show client name Posted in:"? echo $datetime? >//Display message publication time <br> >//display Customer message emoticons pictures? echo $name? " echo $text; >//display customer message content <br>? echo $reply? >//display reply content <br> <a Href= "? echo $home? "target=" _blank "> visit"? Echo$name the home page of the </a>//Customer home page of the hyper-connect <a Href= "mailto:<? echo $email? " echo $name? > </a>//client e-mail connection? What is the QQ number of Echo $name? echo $qq? >//display the customer's QQ number? What is the IP address of Echo $name? echo $ip? "//Show client's IP address <a href=" reply.php?time=<? echo $datetime? "> reply </a>//message reply to the connection statement <a Href=" del.php?time=<? echo $datetime? "> delete </a>//message deleted statements ($datetime as deletion marks with customer message time) <br>. if ($number = = $num-1)//If the cell number of the array is equal to the total number of messages minus one (because the unit number starts with zero, so this means that this is the last message) {break;//jumps out of the loop} $number = $number + 1; Array cell number plus 1}//loop terminator} if ($page <> 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 $back echo "<a Href=index.php? page=1> first page </a> ";//Display the first page of the connection echo" <a Href=index.php?page= $back > prev </a> "; The current page digital number equals $back and displays its connection} if ($page <> $total)// If the current page 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 give the variable $next echo "<a Href=index.php?page= $next > next page </a>";//Display next page Connect echo "<a Href=index.php?page= $total > last page </a>"; Show last page connection} echo "pages: $page/$total";//Display the current page digital number and display the last page of the digital echo "A total of $num message";//Display message number} else {echo "<center> there are currently no messages! </center";//if the contents of the file are empty, the message displayed} else {echo" <center> data file is missing, please contact the administrator! " or post a message to re-establish the data file! </center> ";//If the file does not exist the displayed information}

3. Data Modification Program Section

$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 give the variable $n if ($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 the loop if (eregi ($ttime, $list [$i])) {//will send the message time $ttime and the contents of the array unit $list string matching comparison $f =explode ("", $ list[$i]);//If a match is found, use "" as the delimiter, open the message $list[$i] (section $i) and assign the data to the array $f $f [9]= $GB _reply;//will $f[9] (last data for message message) $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]." \ n "; Replace the contents of the array cell $list[$i] with the array $f with the delimiter "" (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 Gb.dat loop terminator for each cell of the array}// Fclose ($FP);//Close File ShowMessage ("Reply succeeded!", "index.php");//reply successful, automatically return to the main interface.

4. Data Removal Program segment

$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 give the variable $n if ($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 for ($i =0; $i $n; $i + +) {//Enter the loop if (eregi ($ttime, $list [$i])) {//will send the message time $ttime and the string in the array $list[$i] to match the comparison $list [$i]=];//If the match succeeds, $list[$i] Empty (for deletion purposes) break;//jump out of Loop}}//loop terminator for ($i =0; $i <= $n; $i + +) {//Enter loop fwrite ($fp, $list [$i]);//The array $list each cell as a row, Write 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 Procedure 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=" "> <input name=" submit "type=" image "Src=" image/search.gif "alt=" message search "> </font> </td> tr> </table> </form>////////////////////////////////above is the search form statement segment? $id = 0; $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 variables $n $found =trim ($found); if (! $found) {//If $found is false echo ' <center> you have not entered any keywords! </center> ";//Related display} else {if ($n >0) {//If the number of messages is greater than 0 for ($i =0; $i" $n; $i + +) {//Enter the loop if (eregi ($found, $list [$i])) {//Enter the keyword with array $ list[$i] The matching comparison $row =explode ("", $list [$i]); $id = 1; If a match is found, use "" as the 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;//$row the cell data in the array to the variable in parentheses in order? >  >//display customer image Image <br> <font color= "#0099CC "> nickname"? echo $name? ><font size= "2" > "<br>//Show client name Posted in:"? echo $datetime? >//Display message publication time <br> >//display Customer message emoticons pictures? echo $name? " echo $text; >//display customer message content <br>? echo $reply? >//display reply content <br> <a Href= "? echo $home? "target=" _blank "> visit"? echo $name? > homepage of the </a>//customer home page of hyperlinks <a Href= "mailto:<? echo $email? " echo $name? > </a>//client e-mail connection? What is the QQ number of Echo $name? echo $qq? >//display the customer's QQ number? What is the IP address of Echo $name? echo $ip? "//Show client's IP address <a href=" reply.php?time=<? echo $datetime? "> reply </a>//message reply to the connection statement <a Href=" del.php?time=<? echo $datetime? "> delete </a>//message deleted statements ($datetime as deletion marks with customer message time) <br>. }}//loop Terminator}} if ($id ==0) {echo ' <center> did not find a message matching the keyword! </center> ";} If $id=0 indicates that no match is found, display the relevant hints

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.