-
- Var $ file;
- Var $ index;
- // Create a file and write it into the input.
- Function null_write ($ new ){
- $ F = fopen ($ this-> file, "w ");
- Flock ($ f, LOCK_EX );
- Fputs ($ f, $ new );
- Fclose ($ f );
- }
- // Add data records to the end of the file
- Function add_write ($ new ){
- $ F = fopen ($ this-> file, "");
- Flock ($ f, LOCK_EX );
- Fputs ($ f, $ new );
- Fclose ($ f );
- }
- // Used together with the returned results of readfile () to convert a row of data into a one-dimensional array
- Function make_array ($ line ){
- $ Array = explode ("\ x0E", $ line );
- Return $ array;
- }
- // Convert a row of data into a one-dimensional array
- Function join_array ($ line ){
- $ Array = join ("\ x0E", $ line); return $ array;
- }
- // Total number of returned data files
- Function getlines (){
- $ F = file ($ this-> file );
- Return count ($ f );
- }
- // Return the data record of the next row (standby)
- Function next_line (){
- $ This-> index = $ this-> index ++;
- Return $ this-> get ();
- }
- // Return the data record of the previous row (standby)
- Function prev_line (){
- $ This-> index = $ this-> index --;
- Return $ this-> get ();
- }
- // The returned data record for the current row is small
- Function get (){
- $ F = fopen ($ this-> file, "r ");
- Flock ($ f, LOCK_SH );
- For ($ I = 0; $ I <= $ this-> index; $ I ++ ){
- $ Rec = fgets ($ f, 1024 );
- }
- $ Line = explode ("\ x0E", $ rec );
- Fclose ($ f );
- Return $ line;
- }
- // The returned data of the current row is large.
- 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 = explode ("\ x0E", $ rec );
- Fclose ($ f );
- Return $ line;
- }
- // Open the data file --- return the file content in a one-dimensional array
- Function read_file (){
- If (file_exists ($ this-> file )){
- $ Line = file ($ this-> file );
- }
- Return $ line;
- }
- // Open the data file --- return the file content in a 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;
- }
- // Input an array, merge it into a row of data, and overwrite the entire file
- Function overwrite ($ array ){
- $ Newline = implode ("\ x0E", $ array );
- $ F = fopen ($ this-> file, "w ");
- Flock ($ f, LOCK_EX );
- Fputs ($ f, $ newline );
- Fclose ($ f );
- }
- // Add a row of data records to the end of the file
- Function add_line ($ array, $ check_n = 1 ){
- $ S = implode ("\ x0E", $ array );
- $ F = fopen ($ this-> file, "");
- Flock ($ f, LOCK_EX );
- Fputs ($ f, $ s );
- If ($ check_n = 1)
- Fputs ($ f, "\ n ");
- Fclose ($ f );
- }
- // Insert a data record to the beginning of the file
- 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 qualified data records, applicable to the large data size in each row
- 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 $ List = explode ("\ x0E", $ fc [$ I]);
- If ($ list [$ column]! = $ Query_string ){
- $ Newfile = $ newfile. chop ($ fc [$ I]). "\ n ";
- } Else {
- $ Newfile = $ newfile. $ update_string;
- }
- }
- Fclose ($ f );
- $ F = fopen ($ this-> file, "w ");
- Flock ($ f, LOCK_EX );
- Fputs ($ f, $ newfile );
- Fclose ($ f );
- }
- // Update all data records that meet the conditions, applicable to the case where each row has small bytes of data
- Function update2 ($ column, $ query_string, $ update_array ){
- $ Newline = implode ("\ x0E", $ update_array );
- $ Newfile = "";
- $ F = fopen ($ this-> file, "r ");
- Flock ($ f, LOCK_SH );
- 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 );
- }
- // Delete all data records that meet the conditions, which is suitable for large bytes of data in each row.
- Function delete ($ column, $ query_string ){
- $ Newfile = "";
- $ Fc = file ($ this-> file );
- $ F = fopen ($ this-> file, "r ");
- Flock ($ f, LOCK_SH );
- For ($ I = 0; $ I $ List = explode ("\ x0E", $ fc [$ I]);
- If ($ list [$ column]! = $ Query_string ){
- $ Newfile = $ newfile. chop ($ fc [$ I]). "\ n ";
- }
- }
- Fclose ($ f );
- $ F = fopen ($ this-> file, "w ");
- Flock ($ f, LOCK_EX );
- Fputs ($ f, $ newfile );
- Fclose ($ f );
- }
- // Delete all data records that meet the conditions, applicable to the case where the data in each row is small
- Function delete2 ($ column, $ query_string ){
- $ Newfile = "";
- $ F = fopen ($ this-> file, "r ");
- Flock ($ f, LOCK_SH );
- While ($ line = fgets ($ f, 1024 )){
- $ TmpLine = explode ("\ 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 );
- }
- // Obtain the maximum value of a field in a file
- Function get_max_value ($ column ){
- $ Tlines = file ($ this-> file );
- For ($ I = 0; $ I <= count ($ tlines); $ I ++ ){
- $ Line = explode ("\ x0E", $ tlines [$ I]);
- $ Get_value [] = $ line [$ column];
- }
- $ Get_max_value = max ($ get_value );
- Return $ get_max_value;
- }
- // Query based on whether a field in the data file contains $ query_string. all qualified data is returned using a two-dimensional array.
- Function select ($ column, $ query_string ){
- $ Tline = $ this-> openfile ();
- $ Lines = array ();
- Foreach ($ tline as $ line ){
- If ($ line [$ column] ==$ query_string ){
- Array_push ($ lines, $ line );
- }
- }
- Return $ lines;
- }
- // Functions are the same as function select (), and the speed may be slightly improved
- 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;
- }
- // Query whether a field in the data file contains $ query_string. the first qualified data is returned in a one-dimensional array.
- Function select_line ($ column, $ query_string ){
- $ Tline = $ this-> read_file ();
- Foreach ($ tline as $ 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 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] ==$ 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]);
- Return $ down_line;
- }
- ?>
-
|