PHP using txt text file to do database program _php tutorial

Source: Internet
Author: User
Define ("Tblpath", ".");
Define ("Exten", ". php");
Define ("Filehead", " This file is 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; The full file name
var $isModify = false; Whether the current record has been modified
var $fileModify = false; Whether the database has been modified
var $innerRecorders; Database record Array
var $curLine; Current record number
var $curArray; Current row Array
var $stringDel; Save deleted records
var $sprt 1; Delimiter between database records
var $sprt 2; Delimiter between database fields
var $innerBof = true;
var $innerEof = false;

function Create ($tblName, $fields, $sprt 1= "<---txttbl--->n", $sprt 2= "<---txttbl--->") {
if (empty ($tblName)) {
echo "The Textdatebase file name is 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 ($sprt 2, $fields);
$cont = Filehead. " n ". $cont;
$fp = fopen ($fullName, "w");
Fwrite ($fp, $cont);
Fclose ($FP);
return true;
}

function Drop ($tblName, $sprt 1= "<---txttbl--->n", $sprt 2= "<---txttbl--->") {
if (empty ($tblName)) {
echo "The Textdatebase file name is 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, $sprt 1= "<---txttbl--->n", $sprt 2= "<---txttbl--->") {
if (empty ($tblName)) {
echo "The Textdatebase file name is 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 ($sprt 1, $readFromFile);
$this->innercount = count ($this->innerrecorders)-1;
$this->innerfields = Explode ($sprt 2, $this->innerrecorders[0]);
$this->innerfieldscount = count ($this->innerfields);

$this->innername = $tblName;
$this->sprt1 = $sprt 1;
$this->sprt2 = $sprt 2;

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 ($th is->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, $sprt 1= "", $sprt 2= "", $sprt 3= "", $sprt 4 = "") {
echo $SPRT 3;
foreach ($this->curarray as $v) {
if ($shownon ==1&&empty ($v)) $v = "Novalue";
echo $sprt 1. $v. $SPRT 2;
}
Echo $SPRT 4;
}

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 changes
function Savemodify () {
if ($this->ismodify) {
$this->innerrecorders[$this->curline]= implode ($this->sprt2, $this->curarray);
$this->ismodify = false;
$this->filemodify= true;
}
}

Initializes the current array of records when the pointer changes
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 would close.";
$this->close ();
return false;
}
return true;
}


Output current record information, designed to be adjusted for trial
function Ddisplay () {
if ($this->innercount==0) return false;
The echo $v of the foreach ($this->innerfields as $v). ----";
echo "
";
The echo $v of the foreach ($this->curarray as $v). ---";
}

Check if 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 that the submitted field names are 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 is not found.";
return-1;
Default
echo "The field is invalid.";
return-1;
}
}

}


http://www.bkjia.com/PHPjc/444999.html www.bkjia.com true http://www.bkjia.com/PHPjc/444999.html techarticle define (Tblpath,.); define (exten,.php); define (Filehead,? Echo You is Wellcome!?. This file is only for class TXTTBL); Class Txttbl {var $innerName =;//database name var $innerCo ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.