P has just learned PHP. because the website space does not support databases, I wrote this class and just came to practice .? /* ThisTextDataClasstxtTblwritenbyridge.jiangmostone@hotmail.comandfinishedin2003.01.01Yourcancopyanduseitwithoutagreedment,
P> When I first learned PHP, I wrote this class because my website space does not support databases. I just came to practice.
/*
This TextData Class txtTbl writen by ridge. jiang mostone@hotmail.com and finished in 2003.01.01
Your can copy and use it without agreedment, It's free. But you can't use it for busness using.
You are also wellcome to modify this code in your mind. Thank for your work and tell me.
This class is for small information database so like classmater databse, they are less than 300 recorders.
If your want store and opreat more than 300 recorders, It's recommandly to use SQL server.
Your may acess the Instance of this Class with bellow function, there recommandly Do NOT use
Inner var and function, because that's unsafe.
Create ()
Drop ()
Open ()
Close ()
Eof ()
Bof ()
Prev ()
Next ()
First ()
End ()
FieldsCount ()
GetValue ()
SetValue ()
Display ()
Location ()
RecNO ()
RecCount ()
Del ()
Append ()
*/
Define ("tblPath ",".\\");
Define ("exten", ". php ");
Define ("fileHead "," This file only for class txtTbl ");
Class txtTbl {
Var $ innerName = ""; // database name
Var $ innerCount; // number of database records
Var $ innerFields; // database field list array
Var $ inner_F_Count; // number of database fields
Var $ fullName; // Complete file name
Var $ isModify = false; // whether the current record is modified
Var $ fileModify = false; // whether the database is modified
Var $ innerRecorders; // database record array
Var $ curLine; // Current record number
Var $ curArray; // array of the current row
Var $ stringDel; // Save the deleted record
Var $ sprt1; // delimiter between database records
Var $ sprt2; // delimiter between database fields
Var $ innerBof = true;
Var $ innerEof = false;
Function create ($ tblName, $ fields, $ sprt1 = "<--- txtTbl ---> \ n", $ sprt2 = "<--- txtTbl ---> "){
If (empty ($ tblName )){
Echo "The textDateBase file name not appoint .";
Return false;
}
$ FullName = tblPath. $ tblName. exten;
If (file_exists ($ fullName )){
Echo "The textDateBase file is already exist .";
Return false;
}
If (empty ($ fields )){
Echo "The fields list Array is invalid .";
Return false;
}
$ Cont = implode ($ sprt2, $ fields );
$ Cont = fileHead. "\ n". $ cont;
$ Fp = fopen ($ fullName, "w ");
Fwrite ($ fp, $ cont );
Fclose ($ fp );
Return true;
}
Function drop ($ tblName, $ sprt1 = "<--- txtTbl ---> \ n", $ sprt2 = "<--- txtTbl ---> "){
If (empty ($ tblName )){
Echo "The textDateBase file name not proveid .";
Return false;
}
If (! Empty ($ this-> innerName )){
Echo "Current file not closed, Please close it and try again .";
Return false;
}
$ FullName = tblPath. $ tblName. exten;
If (! File_exists ($ fullName )){
Echo "The textDateBase file not exist .";
Return false;
}
$ Fp = fopen ($ fullName, "r ");
If (! Feof ($ fp )){
$ ReadFromFile = fgets ($ fp );
}
If ($ readFromFile! = FileHead. "\ n "){
Fclose ($ fp );
Echo "not a valid textDataBase file. (the head is invalid.)". "\ n ";
Return false;
}
$ ReadFromFile = "";
If (! Feof ($ fp) $ readFromFile. = fgets ($ fp );
Fclose ($ fp );
$ ReadFromFile = trim ($ readFromFile );
If (empty ($ readFromFile )){
Echo "not a valid textDataBase file. (can't find fields define .)";
Return false;
}
$ Cont = fileHead. "\ n". $ readFromFile;
$ Fp = fopen ($ fullName, "w ");
Fwrite ($ fp, $ cont );
Fclose ($ fp );
Return true;
}
Function open ($ tblName, $ sprt1 = "<--- txtTbl ---> \ n", $ sprt2 = "<--- txtTbl ---> "){
If (empty ($ tblName )){
Echo "The textDateBase file name not proveid .";
Return false;
}
If (! Empty ($ this-> innerName )){
Echo "Current file not closed, Please close it and try again .";
Return false;
}
$ This-> fullName = tblPath. $ tblName. exten;
If (! File_exists ($ this-> fullName )){
Echo "The textDateBase file not exist .";
Return false;
}
$ Fp = fopen ($ this-> fullName, "r ");
If (! Feof ($ fp )){
$ ReadFromFile = fgets ($ fp );
}
If ($ readFromFile! = FileHead. "\ n "){
Fclose ($ fp );
Echo "not a valid textDataBase file. (the head is invalid.)". "\ n ";
Return false;
}
$ ReadFromFile = "";
While (! Feof ($ fp) $ readFromFile. = fgets ($ fp );
Fclose ($ fp );
$ ReadFromFile = trim ($ readFromFile );
If (empty ($ readFromFile )){
Echo "not a valid textDataBase file. (can't find fields define .)";
Return false;
}
$ This-> innerRecorders = explode ($ sprt1, $ readFromFile );
$ This-> innerCount = count ($ this-> innerRecorders)-1;
$ This-> innerFields = explode ($ sprt2, $ this-> innerRecorders [0]);
$ This-> innerFieldsCount = count ($ this-> innerFields );
$ This-> innerName = $ tblName;
$ This-> sprt1 = $ sprt1;
$ This-> sprt2 = $ sprt2;
If ($ this-> innerCount = 0 ){
$ This-> curLine = 0;
$ This-> innerEof = true;
} Else {
$ This-> curLine = 1;
// If ($ this-> innerCount = 1) $ this-> innerEof = true;
If (! $ This-> initRec () return false;
}
Return true;
}
Function close (){
If (empty ($ this-> innerName) return true;
// Save modify
$ IsModify = false;
If ($ this-> isModify ){
$ This-> saveModify ();
$ IsModify = true;
}
If (isset ($ this-> stringDel )){
$ IsModify = true;
$ DelNo = explode (",", $ this-> stringDel );
Foreach ($ delNo as $ no ){
$ No = (integer) $ no;
Unset ($ this-> innerRecorders [$ no]);
}
}
If ($ isModify | $ this-> fileModify ){
$ Recorders = implode ($ this-> sprt1, $ this-> innerRecorders );
$ Recorders = fileHead. "\ n". $ recorders;
$ Fp = fopen ($ this-> fullName, "w ");
Fwrite ($ fp, $ recorders );
Fclose ($ fp );
}
$ This-> innerName = "";
Unset ($ this-> innerRecorders );
Unset ($ this-> curArray );
}
Function next (){
If ((! $ This-> innerEof )&&(! Empty ($ this-> innerName ))){
If ($ this-> curLine ==$ this-> innerCount ){
$ This-> innerEof = true;
Return true;
}
$ This-> saveModify ();
$ This-> curLine ++;
If ($ this-> innerBof) $ this-> innerBof = false;
$ This-> initRec ();
}
Return false;
}
Function prev (){
If ((! $ This-> innerBof )&&(! Empty ($ this-> innerName ))){
$ This-> saveModify ();
$ This-> curLine --;
If ($ this-> curLine = 1)
$ This-> innerBof = true;
If ($ this-> innerEof) $ this-> innerEof = false;
$ This-> initRec ();
}
}
Function first (){
If ($ this-> innerBof | empty ($ this-> innerName ))
Return false;
$ This-> saveModify ();
$ This-> curLine = 1;
$ This-> innerBof = true;
$ This-> innerEof = false;
$ This-> initRec ();
}
Function end (){
If ($ this-> innerEof | empty ($ this-> innerName ))
Return false;
$ This-> saveModify ();
$ This-> curLine = $ this-> innerCount;
$ This-> innerEof = true;
$ This-> innerBof = false;
$ This-> initRec ();
}
Function eof (){
If (empty ($ this-> innerName )){
Return false;
} Else return $ this-> innerEof;
}
Function bof (){
If (empty ($ this-> innerName )){
Return true;
} Else return $ this-> innerBof;
}
Function recNo (){
Return $ this-> curLine;
}
Function recCount (){
Return $ this-> innerCount;
}
Function fieldsCount (){
If (empty ($ this-> innerName )){
Return false;
} Else return $ this-> inner_F_Count;
}
Function getValue ($ field ){
If ($ this-> curLine = 0 | empty ($ this-> innerName )){
Echo "Can't read current record, maybe not in use or no record .";
Return false;
}
$ Field = $ this-> chkField ($ field );
If ($ field =-1 ){
Return false;
}
Return $ this-> curArray [$ field];
}
Function setValue ($ field, $ value ){
If ($ this-> curLine = 0 | empty ($ this-> innerName )){
Echo "Can't read current record, maybe not in use or no record .";
Return false;
}
$ Field = $ this-> chkField ($ field );
If ($ field =-1 ){
Return false;
}
$ This-> curArray [$ field] = $ value;
$ This-> modify = true;
}
Function display ($ shownon = 0, $ sprt1 ="", $ Sprt2 ="", $ Sprt3 ="", $ Sprt4 =""){
Echo $ sprt3;
Foreach ($ this-> curArray as $ v ){
If ($ shownon = 1 & empty ($ v) $ v = "noValue ";
Echo $ sprt1. $ v. $ sprt2;
}
Echo $ sprt4;
}
Function location ($ field, $ keyValue ){
$ Field = $ this-> chkField ($ field );
If ($ field =-1) return false;
For ($ I = $ this-> curLine; $ I <= $ this-> innerCount; $ I ++ ){
If ($ this-> curArray [$ field] ==$ keyValue ){
Return true;
}
$ This-> next ();
}
Return false;
}
Function del ($ recNo =-1 ){
If ($ this-> curLine = 0) return false;
$ Vartype = gettype ($ recNo );
If ($ vartype! = "Integer "){
Echo "del error: check ur para type .";
Return false;
}
If ($ recNo =-1 ){
$ RecNo = $ this-> curLine ;}
Elseif ($ recNo> $ this-> innerCount | $ recNo <1 ){
Echo "del error: out over the rang .";
Return false;
}
If (! $ This-> chkDel ($ recNo )){
If (isset ($ this-> stringDel )){
$ This-> stringDel. = (','. $ recNo );
} Else $ this-> stringDel = (string) $ recNo;
} Else return false;
}
Function append ($ fields = ""){
$ This-> saveModify ();
For ($ I = 1; $ I <= $ this-> innerFieldsCount; $ I ++)
$ NewRec [] = "";
If (! Empty ($ fields )){
Foreach ($ fields as $ k => $ v ){
$ K = $ this-> chkField ($ k );
If ($ k =-1 ){
Return false;
}
$ NewRec [$ k] = $ v;
}
}
$ This-> innerCount ++;
$ This-> curLine = $ this-> innerCount;
$ This-> innerBof = false;
$ This-> innerEof = true;
Unset ($ this-> curArray );
$ This-> curArray = & $ newRec;
$ This-> isModify = true;
}
// Save the modification
Function saveModify (){
If ($ this-> isModify ){
$ This-> innerRecorders [$ this-> curLine] = implode ($ this-> sprt2, $ this-> curArray );
$ This-> isModify = false;
$ This-> fileModify = true;
}
}
// When the pointer changes, the current record array is initialized.
Function initRec (){
$ This-> curArray = explode ($ this-> sprt2, $ this-> innerRecorders [$ this-> curLine]);
If (count ($ this-> curArray )! = $ This-> innerFieldsCount ){
Echo "The Current Recorder fields count unequal to Table's. \ n File will close .";
$ This-> close ();
Return false;
}
Return true;
}
// Output the current record information, designed for debugging
Function ddisplay (){
If ($ this-> innerCount = 0) return false;
Foreach ($ this-> innerFields as $ v) echo $ v ."----";
Echo"
";
Foreach ($ this-> curArray as $ v) echo $ v ."---";
}
// Check whether the record has been deleted
Function chkDel ($ key ){
If (empty ($ key) & $ key! = 0 ){
Echo "the key not appoint .";
Return false;
}
If (! Isset ($ this-> stringDel )){
Return false;
}
If (ereg ("(^ |,)". $ key. "(, | $)", $ this-> stringDel )){
Return true;
}
Return false;
}
// Check whether the field name submitted is valid.
Function chkField ($ field ){
If (empty ($ field) & ($ field! = 0 )){
Echo "the field not appoint .";
Return-1;
}
$ Vartype = gettype ($ field );
Switch ($ vartype ){
Case "integer ":
If ($ field >=$ this-> innerFieldsCount ){
Echo "the field is large than fieldscount ";
Return-1;
} Elseif ($ field <0 ){
Echo "the field is less than 0 ";
Return-1;
}
Return $ field;
Case "string ":
Foreach ($ this-> innerFields as $ k => $ v) if ($ field = $ v) return $ k;
Echo "the field name not found .";
Return-1;
Default:
Echo "the field is invalid .";
Return-1;
}
}
}
?>