您好,欢迎访问一九零五行业门户网

Text_data 一个PHP文本数据表类

数据
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=
,$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;$iinnercount;$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  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;$iinnerfieldscount;$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;
  }
//保存修改
 function savemodify(){
  if($this->ismodify){
   $this->innerrecorders[$this->curline]= implode($this->sprt2,$this->curarray);
   $this->ismodify = false;
   $this->filemodify= true;
  }
 }
 //当指针发生变化时,初始化当前记录数组
 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;
 }
//输出当前记录信息,设计为调试用
 function ddisplay(){
  if ($this->innercount==0) return false;
  foreach($this->innerfields as $v) echo $v.----;
  echo
;
  foreach($this->curarray as $v) echo $v.---;
 }
 //检查记录是否已被删除
 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;
 }
 //检查提交的字段名是否合法.
 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     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;
  }
 }
}
?>
其它类似信息

推荐信息