- var $file;
- var $index;
- Create a file and write the input
- function Null_write ($new) {
- $f =fopen ($this->file, "w");
- Flock ($f, LOCK_EX);
- Fputs ($f, $new);
- Fclose ($f);
- }
- Add data record to end of file
- function Add_write ($new) {
- $f =fopen ($this->file, "a");
- Flock ($f, LOCK_EX);
- Fputs ($f, $new);
- Fclose ($f);
- }
- Used with the return of ReadFile () to convert a row of data to a one-dimensional array
- function Make_array ($line) {
- $array = Explode ("\\x0E", $line);
- return $array;
- }
- Converts a row of data for a one-dimensional array
- function Join_array ($line) {
- $array = Join ("\\x0E", $line); return $array;
- }
- Returns the total number of data file rows
- function Getlines () {
- $f =file ($this->file);
- return count ($f);
- }
- Returns the data record for the next row (alternate)
- function Next_line () {
- $this->index= $this->index++;
- return $this->get ();
- }
- Returns the data record for the previous row (alternate)
- function Prev_line () {
- $this->index= $this->index--;
- return $this->get ();
- }
- Returns the data record data 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;
- }
- Returns the data record data for 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 Data File---Return file contents in one-dimensional array
- function Read_file () {
- if (file_exists ($this->file)) {
- $line =file ($this->file);
- }
- return $line;
- }
- Open Data File---Return file contents 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;
- }
- Pass in an array, merge into one row of data, rewrite 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 a file
- 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 row of data records to the front 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 eligible data records for large byte-per-row 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
- $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 eligible data records for smaller byte data per row
- 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 eligible data records for large byte-per-row data
- 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 eligible data records for smaller byte data per row
- 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);
- }
- Get 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;
- }
- Returns all eligible data in a two-dimensional array, based on whether a field in the data file contains $query_string
- 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 like function Select (), 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;
- }
- Returns the first qualifying data in a one-dimensional array, based on whether a field in the data file contains $query_string
- 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;
- }
- ?>
Copy Code
|