PHP
Class CTBClass {
Var $ file; var $ index;
/ / Create a file and write input function null_write ($ new) {$ f = fopen ($ this-> file, "w"); flock ($ f, lock_ex); FPUTS ($ F, $ New); fclose ($ f);} // Add data record to file end function add_write ($ new) {$ f = fopen ($ this-> file, "a"); flock ($ f, lock_ex); fputs ($ F, $ new); fclose ($ f);} // Team to use readfile () to convert a line of data to a line_Array ($ line) {$ array = expedition ("/ x0e", $ line) Return $ array;}
/ / Convert a row of data in a one-dimensional array ($ line) {$ array = join ("/ x0e", $ line); return $ array;} // Return to the total number of data files function getLines () {$ f = file ($ this-> file); return count ($ f);} // Return to the next line of data record (standby) Function Next_Line () {$ this-> index = $ this-> index ; return $ this -> get ();
/ / Return to the data record (standby) function prev_Line () {$ this-> index = $ this-> index ---; return $ this-> get ();} // Return to the data record data of the current line Small function get () {$ f = fopen ($ this-> file, "r"); flock ($ f, lock_sh); for ($ I = 0; $ i <= $ this-> index; $ i ) {$ REC = FGETS ($ F, 1024); $ line = expedition ("/ x0e", $ REC); fclose ($ f); return $ line;} // Return to the current line of data log data larger function GET_BIG_FILE () {$ f = fopen ($ this-> file, "r"); flock ($ f, lock_sh); for ($ I = 0; $ I <= $ this-> index; $ i ) {$ REC = fgets ($ f, 1024 * 5); $ line = expedition ("/ x0e", $ REC); fclose ($ f); return $ line;} // Open data file --- with a one-dimensional array Return to file content function read_file () {if ($ this-> file) {$ line = file ($ this-> file);} Return $ line;} // Open data file --- with two-dimensional array Return file content Function openfile () {ix ($ this-> file) {$ f = file ($ this-> file); $ LINES = array (); foreach ($ f AS $ rawline) {$ TMPLINE = EXPLODE ("/ x0e", $ rawline); Array_Push ($ LINES, $ TMPLINE);}} Return $ LINES;} // Incoming an array, incorporating a row of data, rewriting the entire file Function Overwrite ($ array) {$ Newline = Implode ("/ x0e", $ array); $ f = fopen ($ this-> file, "w"); flock ($ f, lock_ex); FPUTS ($ F, $ NEWLI) Ne); fclose ($ f);} // Add a line of data record to the file end function add_line ($ Array, $ Check_n = 1) {$ s = impode ("/ x0e", $ array); $ f = fopen $ this-> file, "a"); FLOCK ($ F, LOCK_EX); FPUTS ($ F, $ S); if ($ Check_n == 1) FPUTS ($ F, "/ N"); fclose ($ f);}
// Insert a row of data record to the front of the file function insert_line ($ arroy) {$ newfile = Implode ("/ x0e", $ array); $ f = fopen ($ this-> file, "r"); flock ($ f, lock_sh); while ($ LINE = FGETS ($ F, 1024)) {$ newfile. = $ line;} fclose ($ f); $ f = fopen ($ this-> file, "w"); flock ($ F, LOCK_EX); FPUTS ($ F, $ NEWFILE); Fclose ($ f);} // Update all eligible data records, suitable for the larger case data, FUNCTION UPDATE ($ Column, $ query_string, $ update_Array) {$ update_string = Implode ("/ x0e", $ update_array); $ newfile = ""; $ fc = file ($ this-> file); $ f = fopen ($ this-> file, "r"); flock ($ f, lock_sh); for ($ I = 0; $ i
// Update all eligible data records, suitable for each line byte data ($ column, $ query_string, $ update_array) {$ newline = Implode ("/ x0e", $ update_Array); $ newfile = ""; $ f = fopen ($ this-> file, "r"); flock ($ f, lock_sh); while ($ tME = fgets ($ f, 1024)) {$ tmpline = evLode ("/ x0e ", $ line); if ($ TMPLINE [$ column] == $ query_string) {$ newfile. = $ newline;} else {$ newfile. = $ line;}} fclose ($ f); $ f = fopen $ this-> file, "w"); flock ($ f, lock_ex); FPUTS ($ f, $ newfile); fclose ($ f);}
// Remove all qualified data records, which is suitable for the case of each line byte data in Function delete ($ newfile = "; $ fc = file ($ this-> file); $ f = fopen ($ THIS-> File, "R"); FLOCK ($ F, LOCK_SH); for ($ I = 0; $ I
// Get the maximum value of a field in a file Function get_max_value ($ tlines = file ($ this-> file); for ($ i = 0; $ i <= count ($ tlines); $ i ) {$ line = expedition ("/ x0e", $ tlines [$ I]); $ get_value [] = $ get_max_value = max ($ get_value); return $ get_max_value;}
/ / Returns all eligible data Function Select ($ column, $ query_string) based on a field of data files, and returning all eligible data in two-dimensional array. Array (); Foreach ($ TLINE AS $ LINE) {IF ($ LINE [$ column] == $ query_string) {Array_Push ($ LINES, $ LINE);}}
Return $ LINES;
// Function as Function Select (), the speed may have a slight increase in Function SELECT2 ($ column, $ query_string) {if (file_exists ($ this-> file) {$ tline = $ this-> read_file (); foreach $ TLINE AS $ TMPLINE) {$ line = $ this-> make_array ($ tmpline); if ($ LINE [$ column] == $ query_string) {$ LINES [] = $ tmpline;}}} return
/ / Whether a field of data file contains $ query_string query, returning the first eligible data in a single-dimensional array ($ column, $ query_string) {$ tline = $ this-> read_file (); foreach ($ tline = $ tmpline) {$ line = $ this-> make_array ($ tmpline); if ($ line [$ column] == $ query_string) {Return $ Line; Break;}}} // select Next / Prev line (next_prev ==> 1 / next, 2 / prev) by cx function select_next_prev_line ($ column, $ query_string, $ next_prev) {$ tline = $ this-> read_file (); $ line_key_end = count ($ tline) - 1 $ line_key = -1; Foreach ($ tline) {$ line_key ; $ line = $ this-> make_array ($ tmpline); if ($ next_prev == 1) {// next? if ($ LINE [ $ column] == $ query_string) {if ($ line_key == 0) {return 0;} else} else {$ line_key_up = $ line_key - 1; return $ up_line;}} else} else} elseif $ Next_PREV == 2) {// prev? IF ($ LINE [$ column] == $ query_string) {if ($ line_key == $ line_key_end) {return 0;} else {$ line_key_down = $ line_key 1; Break }}} else {return 0;}} $ down_line = $ this-> make_Array ($ tline [$ line_key_down]); RET URN $ DOWN_LINE;}?>