php 用txt文本文件做数据库程序_PHP教程
define ("exten",".php");
define ("fileHead"," echo "You are wellcome!"?".">This file only for class txtTbl");
class txtTbl {
var $innerName=""; //数据库名称
var $innerCount; //数据库记录数目
var $innerFields; //数据库字段列表数组
var $inner_F_Count; //数据库字段数目
var $fullName; //完整的文件名
var $isModify = false; //当前记录是否被修改
var $fileModify = false; //数据库是否被修改
var $innerRecorders; //数据库记录数组
var $curLine; //当前记录号
var $curArray; //当前行数组
var $stringDel; //保存被删除记录
var $sprt1; //数据库记录间的分隔符
var $sprt2; //数据库字段间的分隔符
var $innerBof = true;
var $innerEof = false;
function create($tblName,$fields,$sprt1="n",$sprt2=""){
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="n",$sprt2=""){
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="n",$sprt2=""){
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="