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 log to file end function add_write ($ new) {$ f = fopen ($ this-> file, "a"); flock $ F, LOCK_EX); FPUTS ($ F, $ New); fclose ($ f);} // Troubled with ReadFile () to convert a line of data to a one-dimensional array Function Make_Array ($ line) {$ array = EXPLODE ("/ x0e", $ line); Return $ array;} // converts a row of lines for 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 previous line of data record (备) Function prev_Line ($ this-> index = $ THIS -> INDEX ---; Return $ this-> get ();
} // Return to the current line of data record data 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); returnid;} / / Return 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); returnid;} / / Open data file --- Return to file in a single-dimensional array Function read_file () {if ($ this-> file) {$ line = file ($ this-> file);} returnid;} // Open data file --- Return file content in two-dimensional array Function OpenFile () {if (File_exists ($ 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, merged into a line of data , Rewrite the entire file Function Overwrite ($, $ newline = Implode ("/ x0e", $ array); $ f = fopen ($ this-> file, "w"); flock ($ f, lock_ex); FPUTS ($ f, $ newline); fclose ($ f);} // Add a line of data record to the file end function add_line ($ array, $ check_n = 1) {$ s = IMPLODE ("/ 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 line of data record to the front Function INSERT_LINE ($ array) {$ 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 case of each line byte 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
While ($ LINE = FGETS ($ f, 1024)) {$ TMPLINE = Explode ("/ 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 eligible data records, suitable for the case of each line byte data in Function delete ($ column, $ query_string) {$ newfile = ""; $ fc = file ($ 5) This-> file); $ f = fopen ($ this-> file, "r"); flock ($ f, lock_sh); for ($ i = 0 $ I
} // Remove all qualified data records, which is suitable for the smaller case of each line byte data ($ column, $ query_string) {$ newfile = "; $ f = fopen ($ this-> file," R "); FLOCK ($ f, lock_sh); while ($ line = fgets ($ f, 1024)) {$ tmpline = expedition (" / x0e ", $ line); if ($ TMPLINE [$ column]! = $ query_string) {$ newfile. = $ line;}} fclose ($ f); $ f = fopen ($ this-> file, "w"); flock ($ f, lock_ex); FPUTS ($ f, $ newfile ); Fclose ($ f);} // get the maximum value of a field in a file Function get_max_value ($ tlines = file ($ this-> file); for ($ i = 0; $ i < = CO UND ($ tLINES); $ i ) {$ line = evtern ("/ x0e", $ tlines [$ i]); $ get_value [] = $ line [$ column];} $ get_max_value = max ($ get_value); RETURN $ GET_MAX_VALUE;} // Whether to query $ query_string according to a field of the data file, returning all eligible data in a two-dimensional array ($ column, $ query_string) {$ tline = $ this-> OpenFile $ LINES = array ();
Foreach ($ TLINE AS $ LINE) {IF ($ LINE [$ Column] == $ query_string) {Array_Push ($ LINES, $ LINE);}}} Return $ LINES;} // Function SELECT (), speed Maybe a lot of 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 $ LINES;} // Whether to contain $ query_string in a field, return the first eligible data in a single-dimensional array ($ column, $ query_string) {$ tline = $ this-> Read_File (); Foreach ($ tline) {$ 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 as $ tmpline) {$ line_key ; $ line = $ this-> make_Array ($ tmpline); if ($ next_prev == 1) { // next? IF ($ Line [$ column] == $ query_string) {if ($ line_key == 0) {return 0;} else { $ Line_key_up = $ line_key - 1; return $ up_line;}} else {$ up_LINE = $ line;}} elseif ($ next_prev == 2) {// prev? If ($ LINE [$ column] ==