PHP implementation of the five basic operations of the text database method _php Tutorial

Source: Internet
Author: User
Source: http://www.pchoer.com/

PHP implementation of the text database data display, add, modify, delete, query five basic methods of operation
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");//Get system time
$ip = $HTTP _server_vars[remote_addr]; IP address of the speaker
$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. ""; /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 a file
Fclose ($FP);//Close file
ShowMessage ("Message success!", "index.php", "3");//Message success, 3 seconds after the automatic return to the main screen.
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")) {//detect if file exists
$array =file ("Gb.dat");//reads the entire contents of the file into the array $array
$arr =array_reverse ($array);//$array The data in the row (i.e. the last line of the first row, and so on) reads into each cell 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 up, $pagesize the amount of information displayed per page for a preset)
if ($page <1) {//If the current page digital 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 from zero, mainly to achieve the purpose of corresponding to the array unit number)
for ($i =0; $i <= $pagesize-1; $i + +) {//Enter loop
$row =explode ("|", $arr [$number]);//with "|" As a delimiter, splits the data for each of the cells in the array $arr and assigns 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 in order to the variable in parentheses
?>
>//Display image of customer


Nickname 】
Show Customer Name
Posted in: Show message release time


>//Display Customer Message emoticons image
Said: Show customer message Content


Show reply content


"target=" _blank "> Access Hyperlinks to the home//customer homepage
"> Gives Send//Customer e-mail connection
The QQ number is Show customer's QQ number
The IP address is "//Display the customer's IP address
"> Reply//message reply to the connection statement
"> Delete//message DELETE statement (with customer message time $datetime as deletion logo)


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;//jump 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 "First page";//Display the first page of the connection
echo "Prev"; The current page is a digital number equal to $back and displays 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 $next
echo "Next page";//Display next page connection
echo "last Page"; Show last page connection
}
echo "pages: $page/$total";//Display the current page digital number and display the last page digital number
echo "A total of $num message";//Display message number information

}
else {
echo "

There are no messages at this moment! ";//information displayed if the file contents 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 exist the displayed information
}

3, Data modification 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 of the data .....
$n =count ($list);//Calculates the total number of messages in $list content and assigns variables $n
if ($n >0) {//If the number of messages is greater than 0
$fp =fopen ("Gb.dat", "w");// The file Gb.dat
$gb _reply=encode ($GB _reply) is opened in write-only mode;
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 the string matching comparison
$f = Explode ("|", $list [$i]);//If a match is found, the "|" As a delimiter, cut the message $list[$i] ($i), and assign the data to the array $f
$f [9]= $gb _reply;//$f[9] (the last data for the message) is replaced with $gb_reply (reply to content).
$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 of Loop
}
}//loop Terminator
}
for ($i =0; $i <= $n; $i + +) {//Enter loop
fwrite ($fp, $list [$i]);//will array $ Each cell of the list is a row, written to the file Gb.dat
}//loop Terminator
Fclose ($FP);//Close File
ShowMessage ("Reply succeeded!", "index.php");//reply succeeds, 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 $list content and assign variables $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 loop
if (eregi ($ttime, $list [$i])) {//will be sent over to send message time $ttime match the string in the array $list[$i]
$list [$i]= ";//If the match succeeds, the $list[$i] is emptied (for deletion purposes)
break;//jump out of the loop
}
}//Loop Terminator
for ($i =0; $i <= $n; $i + +) {//Enter loop
Fwrite ($fp, $list [$i]);//$list each cell of the array to a single line, writing 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 Procedure Section

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 $list content and assign variables $n
$found =trim ($found);
if (! $found) {//if $found is False
echo "

You did not enter any keywords! ";//For related display
}
else {
if ($n >0) {//If the number of messages is greater than 0
for ($i =0; $i < $n; $i + +) {//Enter loop
if (eregi ($found, $list [$i])) {//Enter the keyword to match the string in the array $list[$i]
$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;//Assign the cell data in the array $row in order to the variable in parentheses
?>
>//Display image of customer


Nickname 】
Show Customer Name
Posted in: Show message release time


>//Display Customer Message emoticons image
Said: Show Customer

http://www.bkjia.com/PHPjc/532670.html www.bkjia.com true http://www.bkjia.com/PHPjc/532670.html techarticle Source: Http://www.pchoer.com/PHP Realization of the data display, adding, modifying, deleting and querying the five basic operation methods of the text database I use a message this program as an example, elaborated ...

  • Related Article

    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.