Copy Code code as follows:
<?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 log to file end
function Add_write ($new) {
$f =fopen ($this->file, "a");
Flock ($f, LOCK_EX);
Fputs ($f, $new);
Fclose ($f);
}
With the return of ReadFile (), convert one row of data into a one-dimensional array
function Make_array ($line) {
$array = Explode ("\x0e", $line);
return $array;
}
Converts a one-dimensional array to one row of data
function Join_array ($line) {
$array = Join ("\x0e", $line);
return $array;
}
Returns the total number of rows in a data file
function Getlines () {
$f =file ($this->file);
return count ($f);
}
Returns the data record for the next row (standby)
function Next_line () {
$this->index= $this->index++;
return $this->get ();
}
Returns the data record for the previous row (standby)
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 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 =explode ("\x0e", $rec);
Fclose ($f);
return $line;
}
Open a data file---return the contents of a file with a one-dimensional array
function Read_file () {
if (file_exists ($this->file)) {
$line =file ($this->file);
}
return $line;
}
Open a data file---return the contents of a file 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 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);
}
Inserts a row of data 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 larger cases of byte data per 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 <count ($FC); $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 larger cases of byte data per row
function Delete ($column, $query _string) {
$newfile = "";
$FC =file ($this->file);
$f =fopen ($this->file, "R");
Flock ($f, lock_sh);
for ($i =0; $i <count ($FC); $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);
}
Gets 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;
}
function is as simple as function select (), the speed may be slightly elevated
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;
}
}
?>