首页 > 开发 > PHP > 正文

Text_data 一个PHP文本数据表类

2024-05-04 22:59:39
字体:
来源:转载
供稿:网友

<?php
/*
this textdata class txttbl writen by ridge.jiang [email protected] 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","<? 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="<---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="<td>",$sprt2="</td>",$sprt3="<tr>",$sprt4="</tr>"){
 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;
  }
 


 //保存修改
 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 "<br>";
  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<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;
  }
 }

}
?>


 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表