PHP implementation of common methods of working with text databases Demo _php tutorial

Source: Internet
Author: User
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:

<?if (file_exists ("Gb.dat")) {//detects if the file exists $array=file ("Gb.dat");//reads all the contents of the file into the array $array$arr=array_reverse ($array) ;//The data in the $array is inverted (that is, the first row of the last line, and so on) is read into every 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;//, the value is 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 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 "&LT;? echo $name?> "
Show client name posted in: <? echo $datetime? >//Display Message release time
>//Display Customer message emoticons picture <? Echo $name?> said: <? echo $text; >//Display Customer message content
<? echo $reply? >//Display reply content
"Rel=" external nofollow "target=" _blank "> Access <? Echo $name?> 's homepage//Customer homepage HYPERLINK "rel=" External nofollow "> to <? echo $name?>//customer e-mail connection < echo $name?> QQ number is <? echo $qq? >//display the customer's QQ number <? echo $name?> IP address is <? echo $ip?> "//Show client's IP address" rel= "external nofollow" > reply/Message reply connection statement "rel=" external nofollow "> Delete//message DELETE statement (with customer message time $datetime as a delete identity)
<? 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 $backecho "first page";/ Display the first page of the connection echo "previous"; the current page digital number equals $back, and displays its connection}if ($page <> $total)//If the current page digital number is not equal to the total number of pages (last digital number) {$next = $page + 1;// The current page digital number plus 1 and give 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 <? $id =0; $list =file ("Gb.dat");//reads the entire gb.dat file into the array $list, each element of the array is a message ( $list [0] is the first message of the data, $list [1] is the first message data ... $n =count ($list);//Calculates the total number of messages in $list content and assigns the variable $n$found=trim ($found); if (!$ Found) {//If $found is false echo "
 
  
   
  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 "<? echo $name?> "
Show client name posted in: <? echo $datetime? >//Display Message release time
>//Display Customer message emoticons picture <? Echo $name?> said: <? Echo $text; >//Show Customers

http://www.bkjia.com/PHPjc/824625.html www.bkjia.com true http://www.bkjia.com/PHPjc/824625.html techarticle 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, briefly describe the PHP implementation of the number of text ...

  • 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.