Home > Backend Development > PHP Tutorial > PHP implements the five basic operations of data display, adding, modifying, deleting and querying text databases

PHP implements the five basic operations of data display, adding, modifying, deleting and querying text databases

WBOY
Release: 2016-07-25 09:10:55
Original
1233 people have browsed it
PHP implements the five basic operations of data display, adding, modifying, deleting and querying text databases
This text database has a total of 9 fields: private $bankid; //Bank ID private $bankname; //Bank name private $bankimg; //Bank pictures private $bankarea; //Coverage area private $bankcard; //Card types accepted private $banklimit; //Payment limit private $bankpasswd; //Transaction password private $banknote; //Bank information remarks private $bankmiss; //Other information content of the bank.
  1. /**
  2. php implements the five basic operations of data display, adding, modifying, deleting and querying text databases.
  3. This text database has a total of 9 fields:
  4. private $bankid; //Bank ID
  5. private $bankname; //Bank name
  6. private $bankimg; //Bank picture
  7. private $bankarea; //Covered area
  8. private $bankcard; //Accepted card types
  9. private $banklimit; //Payment limit
  10. private $bankpasswd; //Transaction password
  11. private $banknote; //Bank information notes
  12. private $bankmiss; //Other bank information.
  13. @abstract TxtDB store
  14. @access public
  15. @author yuchao1@staff.sina.com.cn
  16. */
  17. class TxtDB {
  18. private $bankid; //Bank ID
  19. private $bankname; //Bank name
  20. private $bankimg; //Bank picture
  21. private $bankarea; //covered area
  22. private $bankcard; //accepted card types
  23. private $banklimit; //payment limit
  24. private $bankpasswd; //transaction password
  25. private $banknote; //bank information remarks
  26. private $ bankmiss; //Other bank information
  27. public function __construct() {
  28. $bankid = ""; //Bank ID
  29. $bankname = ""; //Bank name
  30. $bankimg = ""; //Bank picture
  31. $bankarea = ""; //Covered area
  32. $bankcard = ""; //Accepted card types
  33. $banklimit = ""; //Payment limit
  34. $bankpasswd = ""; //Transaction password type
  35. $banknote = " "; //Bank information notes
  36. $bankmiss = ""; //Other bank information
  37. }
  38. /**
  39. * Add data program segment.
  40. * $bankinfo array List of bank information to be inserted
  41. * $bankinfo["bankid"]$bankinfo["bankname"]$bankinfo["bankimg"]$bankinfo["bankarea"]
  42. * $bankinfo["bankcard"] $bankinfo["banklimit"]
  43. * $bankinfo["bankpasswd"]$bankinfo["banknote"]$bankinfo["bankmiss"]
  44. * @return boolean true on success
  45. * false on failure
  46. */
  47. public static function insert($bankinfo) {
  48. $date = date ( "Y-m-d H: i:s" ); //Get the system time
  49. foreach ( $bankinfo as $key => $value ) {
  50. $key = trim ( $value ); //Remove the spaces after the bank content.
  51. }
  52. try {
  53. $fp = fopen ( "banklist.txt", "a" ); //Open the banklist.txt text file in write-only mode, with the file pointer pointing to the end of the file.
  54. $str = $bankinfo ["bankid"] . " |" . $bankinfo ["bankname"] . "|" . $bankinfo ["bankimg"] . "|" .
  55. $bankinfo ["bankarea"] . "|" . $bankinfo ["bankcard"] . "| " . $bankinfo ["banklimit"] . "|" .
  56. $bankinfo ["bankpasswd"] . "|" . $bankinfo ["banknote"] . "|" . $bankinfo ["bankmiss"] . "|" . $date . "rn";
  57. //Assign all bank data to the variable $str. The purpose of "|" is to use it as the data interval symbol when dividing data in the future.
  58. fwrite ($fp, $str); //Write data to the file
  59. fclose ($fp); //Close the file
  60. //The $banklist is the data passed from the bank form.
  61. return true;
  62. } catch (Exception $e) {
  63. return false;
  64. }
  65. }
  66. public static function show() {
  67. //Data display program segment
  68. if (file_exists ( "banklist.txt" )) { //Check whether the file exists
  69. $array = file ( "banklist.txt" ); // Read the entire contents of the file into the array $array
  70. $arr = array_reverse ( $array ); // Arrange the data in $array Flip the arrangement (i.e. the last row becomes the first row, and so on) and read each element of the array $arr ($arr[0]...).
  71. }
  72. var_dump ( $arr );
  73. }
  74. /**
  75. * Data modification program segment
  76. * $bankinfo array Bank information list to be inserted
  77. * $bankinfo["bankid"]$bankinfo["bankname"]$bankinfo["bankimg"]$bankinfo["bankarea"]
  78. * $ bankinfo["bankcard"]$bankinfo["banklimit"]
  79. * $bankinfo["bankpasswd"]$bankinfo["banknote"]$bankinfo["bankmiss"]
  80. * @return boolean true on success
  81. * false on failure
  82. */
  83. public static function alter($bankinfo) {
  84. $date = date ( "Y-m-d H:i:s" ); //Get System modification time
  85. $list = file ( "banklist.txt" ); //Read the entire banklist.txt file into the array $list, each element of the array is a bank ($list[0] is the data of the first bank , $list[1] is the first bank data....
  86. $n = count ( $list ); // Calculate the total number of banks in the $list content and assign it to the variable $n
  87. foreach ( $bankinfo as $key => $value ) {
  88. $key = trim ( $value ); //Remove the spaces after the bank content.
  89. }
  90. if ($n > 0) { //If the number of banks is greater than 0
  91. $fp = fopen ( "banklist.txt", "w" ); //Open the file banklist.txt in write-only mode
  92. for($i = 0; $i < $n; $i ++) { // Enter the loop
  93. if (eregi ( $bankinfo["bankid"], $list [$i] )) { // String matching and comparison of the passed bankid and the content in the array unit $list
  94. $f = explode ( "| ", $list [$i] ); //If a match is found, use "|" as the delimiter to cut the bank information $list[$i] (the $i-th bank), and assign these data to the array$ f
  95. $f[0] = $bankinfo["bankid"];
  96. $f[1] = $bankinfo["bankname"];
  97. $f[2] = $bankinfo["bankimg"];
  98. $f[ 3] = $bankinfo["bankarea"];
  99. $f[4] = $bankinfo["bankcard"];
  100. $f[5] = $bankinfo["banklimit"];
  101. $f[6] = $bankinfo ["bankpasswd"];
  102. $f[7] = $bankinfo["banknote"];
  103. $f[8] = $bankinfo["bankmiss"];
  104. $f[9] = $date;
  105. $list [ $i] = $f [0] . "|" . $f [1] . "|" . $f [2] . "|" . $f [3] . "|" . $f [4] . "|" . $f [5] . "|" . $f [6] . "|" . $f [7] . "|" . $f [8] . "|" . $f [9] . "rn";
  106. //Replace the contents of the array unit $list[$i] with the array $f plus the separator "|".
  107. break; //Get out of the loop
  108. }
  109. }//End of loop symbol
  110. }
  111. for($i = 0; $i <= $n; $i ++) { //Enter the loop
  112. fwrite ( $fp, $list [$i] ); //Convert each unit of the array $list into one line and write it to the file banklist.txt
  113. } //Loop end character
  114. fclose ( $fp ); // Close the file
  115. }
  116. /* *
  117. * Data deletion program segment
  118. * @param $bankid bank id number
  119. * @return boolean true success
  120. * false failure
  121. *
  122. */
  123. public static function delete($bankid) {
  124. $list = file ( "banklist.txt" ); //Read the entire banklist.txt file into the array $list, each element of the array is a bank ($list[0] is the data of the first bank, $list[1] is the data of the first bank...
  125. $n = count ( $list ); // Calculate the banks in the content of $list The total number and assigned to variable $n
  126. if ($n > 0) { //If the number of banks is greater than 0
  127. $fp = fopen ( "banklist.txt", "w" ); //Open the file in write-only mode banklist.txt
  128. for($i = 0; $i < $n; $i ++) { //Enter the loop
  129. if (eregi ( $bankid, $list [$i] )) { //Will be sent here Match and compare the bank's $bankid with the strings in the array $list[$i]
  130. $list [$i] = ""; //If the match is successful, $list[$i] will be cleared (to achieve the purpose of deletion) )
  131. break; //Jump out of the loop
  132. }
  133. } //Loop end character
  134. FOR($i = 0; $i <= $n; $i ++) { //Enter the loop
  135. fwrite ( $fp, $ list [$i] ); //Convert each unit of the array $list into one line and write it to the file banklist.txt
  136. } //Loop end character
  137. fclose ( $fp ); // Close the file
  138. }
  139. }
  140. /**
  141. * Data query program segment
  142. * @param $bankid bank ID number
  143. * @return boolean returns true on success
  144. * returns false on failure
  145. *
  146. */
  147. public static function select($bankid) {
  148. $id = 0;
  149. $list = file ( "banklist.txt" ); //Read the entire banklist.txt file into the array $list ,
  150. //Each element of the array is a bank ($list[0] is the data of the first bank, $list[1] is the data of the second bank...
  151. $n = count ( $list ); //Calculate the total number of banks in the $list content and assign it to the variable $n
  152. $bankid = trim ( $bankid );
  153. if (! $bankid) { //If $bankid is false
  154. echo "You did not enter anything Keyword! "; //For related display
  155. return false;
  156. } else {
  157. if ($n > 0) { //If the number of banks is greater than 0
  158. for($i = 0; $i < $n; $i + +) { //Enter the loop
  159. if (eregi ( $bankid, $list [$i] )) { // Match and compare the entered keywords with the strings in the array $list[$i]
  160. $row = explode ( "|", $list [$i] );
  161. $id = 1; //If a match is found, use "|" as the delimiter to cut the bank information $list[$i]($ith bank ), and assign these data to the array $row. And assign 1 to the variable $id to determine whether a match is found.
  162. list ($bankid,$bankname,$bankimg,$bankarea,$bankcard,$banklimit,$bankpasswd. ,$banknote,$bankmiss) = $row; //Assign the unit data in the array $row to the variables in the brackets in order
  163. //echo $bankname;
  164. return $row;
  165. }
  166. }//Loop end character
  167. }
  168. }
  169. if ($id == 0) {
  170. echo "No bank matching the keyword was found! ";
  171. return false;
  172. } //If $id=0, it means no match is found and relevant prompts are displayed
  173. }
  174. public function __destruct() {
  175. }
  176. }
  177. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template