Discuz教程网

PHP文本数据表类

[复制链接]
authicon 09927306 发表于 2011-1-6 12:27:38 | 显示全部楼层 |阅读模式
  1. <?
  2. /*
  3. This TextData Class txtTbl writen by ridge.jiang [email=mostone@hotmail.com]mostone@hotmail.com[/email] and finished in 2003.01.01
  4. Your can copy and use it without agreedment,It\'s free. But you can\'t use it for busness using.
  5. You are also wellcome to modify this code in your mind. Thank for your work and tell me.
  6. This class is for small information database so like classmater databse,they are less than 300 recorders.
  7. If your want store and opreat more than 300 recorders,It\'s recommandly to use SQL server.
  8. Your may acess the Instance of this Class with bellow function,there recommandly Do NOT use
  9. inner var and function,because that\'s unsafe.
  10. create()
  11. drop()
  12. open()
  13. close()
  14. eof()
  15. bof()
  16. prev()
  17. next()
  18. first()
  19. end()
  20. fieldsCount()
  21. getValue()
  22. setValue()
  23. display()
  24. location()
  25. recNO()
  26. recCount()
  27. del()
  28. append()
  29. */
  30. define ("tblPath",".\\\\");
  31. define ("exten",".php");
  32. define ("fileHead","<? echo \\"You are wellcome!\\"?".">This file only for class txtTbl");
  33. class txtTbl {
  34.   var $innerName="";  //数据库名称
  35.   var $innerCount;  //数据库记录数目
  36.   var $innerFields;  //数据库字段列表数组
  37.   var $inner_F_Count; //数据库字段数目
  38.   var $fullName;  //完整的文件名
  39.   var $isModify = false;  //当前记录是否被修改
  40.   var $fileModify = false;  //数据库是否被修改
  41.   var $innerRecorders;  //数据库记录数组
  42.   var $curLine;  //当前记录号
  43.   var $curArray;  //当前行数组
  44.   var $stringDel;  //保存被删除记录
  45.   var $sprt1;  //数据库记录间的分隔符
  46.   var $sprt2;  //数据库字段间的分隔符
  47.   var $innerBof = true;
  48.   var $innerEof = false;

  49.   function create($tblName,$fields,$sprt1="<---txtTbl--->\\n",$sprt2="<---txtTbl--->"){
  50. if (empty($tblName)){
  51.   echo "The textDateBase file name not appoint.";
  52.   return false;
  53. }
  54. $fullName = tblPath.$tblName.exten;
  55.     if (file_exists($fullName)){
  56.   echo "The textDateBase file is already exist.";
  57.   return false;
  58. }
  59.     if(empty($fields)){
  60.   echo "The fields list Array is invalid.";
  61.   return false;
  62. }
  63. $cont = implode($sprt2,$fields);
  64. $cont = fileHead."\\n".$cont;
  65. $fp = fopen($fullName,"w");
  66.     fwrite($fp,$cont);
  67. fclose($fp);
  68. return true;
  69.   }
  70.   function drop($tblName,$sprt1="<---txtTbl--->\\n",$sprt2="<---txtTbl--->"){
  71. if (empty($tblName)){
  72.   echo "The textDateBase file name not proveid.";
  73.   return false;
  74. }
  75. if (!empty($this->innerName)){
  76.   echo "Current file not closed,Please close it and try again.";
  77.   return false;
  78. }
  79. $fullName = tblPath.$tblName.exten;
  80.     if (!file_exists($fullName)){
  81.   echo "The textDateBase file not exist.";
  82.   return false;
  83. }
  84. $fp = fopen($fullName,"r");
  85. if (!feof($fp)){
  86.   $readFromFile = fgets($fp);
  87. }
  88. if ($readFromFile!=fileHead."\\n"){
  89.   fclose($fp);
  90.   echo "not a valid textDataBase file.(the head is invalid.)"."\\n";
  91.   return false;
  92. }
  93. $readFromFile = "";
  94. if (!feof($fp)) $readFromFile.= fgets($fp);
  95. fclose($fp);
  96. $readFromFile = trim($readFromFile);
  97. if (empty($readFromFile)){
  98.   echo "not a valid textDataBase file.(can\'t find fields define.)";
  99.   return false;
  100. }
  101. $cont = fileHead."\\n".$readFromFile;
  102. $fp = fopen($fullName,"w");
  103. fwrite($fp,$cont);
  104. fclose($fp);
  105. return true;
  106.   }


  107.   function open($tblName,$sprt1="<---txtTbl--->\\n",$sprt2="<---txtTbl--->"){
  108. if (empty($tblName)){
  109.   echo "The textDateBase file name not proveid.";
  110.   return false;
  111. }
  112. if (!empty($this->innerName)){
  113.   echo "Current file not closed,Please close it and try again.";
  114.   return false;
  115. }
  116. $this->fullName = tblPath.$tblName.exten;
  117.     if (!file_exists($this->fullName)){
  118.   echo "The textDateBase file not exist.";
  119.   return false;
  120. }
  121. $fp = fopen($this->fullName,"r");
  122. if (!feof($fp)){
  123.   $readFromFile = fgets($fp);
  124. }
  125. if ($readFromFile!=fileHead."\\n"){
  126.   fclose($fp);
  127.   echo "not a valid textDataBase file.(the head is invalid.)"."\\n";
  128.   return false;
  129. }
  130. $readFromFile = "";
  131. while (!feof($fp)) $readFromFile.= fgets($fp);
  132. fclose($fp);
  133. $readFromFile = trim($readFromFile);
  134. if (empty($readFromFile)){
  135.   echo "not a valid textDataBase file.(can\'t find fields define.)";
  136.   return false;
  137. }
  138.     $this->innerRecorders = explode($sprt1,$readFromFile);
  139. $this->innerCount = count($this->innerRecorders) - 1;
  140.     $this->innerFields = explode($sprt2,$this->innerRecorders[0]);
  141.     $this->innerFieldsCount = count($this->innerFields);
  142. $this->innerName = $tblName;
  143. $this->sprt1 = $sprt1;
  144. $this->sprt2 = $sprt2;
  145. if ($this->innerCount==0){
  146.   $this->curLine = 0;
  147.   $this->innerEof = true;
  148. }else{
  149.   $this->curLine = 1;
  150. //  if ($this->innerCount==1) $this->innerEof = true;
  151.         if (!$this->initRec()) return false;
  152. }
  153. return true;
  154.   }
  155.   function close(){
  156. if (empty($this->innerName)) return true;
  157. //save modify
  158. $isModify= false;
  159.     if ($this->isModify){
  160.   $this->saveModify();
  161.   $isModify= true;
  162. }
  163. if(isset($this->stringDel)){
  164.   $isModify= true;
  165.   $delNo= explode(",",$this->stringDel);
  166.   foreach($delNo as $no){
  167.    $no= (integer) $no;
  168.    unset($this->innerRecorders[$no]);
  169.   }
  170. }
  171. if ($isModify||$this->fileModify){
  172.   $recorders= implode($this->sprt1,$this->innerRecorders);
  173.   $recorders= fileHead."\\n".$recorders;
  174.   $fp = fopen($this->fullName,"w");
  175.   fwrite($fp,$recorders);
  176.   fclose($fp);
  177. }
  178. $this->innerName="";
  179. unset($this->innerRecorders);
  180. unset($this->curArray);
  181.   }
  182.   function next(){
  183.    if ((!$this->innerEof)&&(!empty($this->innerName))){
  184.     if($this->curLine==$this->innerCount){
  185.      $this->innerEof = true;
  186.      return true;
  187.     }
  188.     $this->saveModify();
  189.     $this->curLine++;
  190.     if ($this->innerBof) $this->innerBof = false;
  191.     $this->initRec();
  192.    }
  193.    return false;
  194.   }

  195.   function prev(){
  196.    if ((!$this->innerBof)&&(!empty($this->innerName))){
  197.     $this->saveModify();
  198.     $this->curLine--;
  199.     if ($this->curLine == 1)
  200.      $this->innerBof = true;
  201.     if ($this->innerEof) $this->innerEof = false;
  202.     $this->initRec();
  203.    }
  204.   }
  205.   function first(){
  206.     if ($this->innerBof||empty($this->innerName))
  207.   return false;
  208.     $this->saveModify();
  209. $this->curLine = 1;
  210. $this->innerBof= true;
  211. $this->innerEof = false;
  212. $this->initRec();

  213.   }

  214.   function end(){
  215.     if ($this->innerEof||empty($this->innerName))
  216.   return false;
  217.     $this->saveModify();
  218. $this->curLine = $this->innerCount;
  219. $this->innerEof= true;
  220. $this->innerBof = false;
  221. $this->initRec();

  222.   }

  223.   function eof(){
  224.    if (empty($this->innerName)){
  225.     return false;
  226.    }else return $this->innerEof;
  227.   }
  228.   function bof(){
  229.    if (empty($this->innerName)){
  230.     return true;
  231.    }else return $this->innerBof;
  232.   }
  233.   function recNo(){
  234.    return $this->curLine;
  235.   }
  236.   function recCount(){
  237.    return $this->innerCount;
  238.   }
  239.   function fieldsCount(){
  240.    if (empty($this->innerName)){
  241.     return false;
  242.    }else return $this->inner_F_Count;
  243.   }
  244.   function getValue($field){
  245. if ($this->curLine==0||empty($this->innerName)){
  246.   echo "Can\'t read current record,maybe not in use or no record.";
  247.   return false;
  248. }
  249. $field= $this->chkField($field);
  250.     if ($field==-1){
  251.   return false;
  252. }
  253. return $this->curArray[$field];
  254.   }

  255.   function setValue($field,$value){
  256.     if ($this->curLine==0||empty($this->innerName)){
  257.   echo "Can\'t read current record,maybe not in use or no record.";
  258.   return false;
  259. }
  260. $field= $this->chkField($field);
  261. if ($field==-1){
  262.   return false;
  263. }
  264. $this->curArray[$field]= $value;
  265. $this->modify= true;
  266.   }

  267.   function display($shownon=0,$sprt1="<td>",$sprt2="</td>",$sprt3="<tr>",$sprt4="</tr>"){
  268. echo $sprt3;
  269. foreach($this->curArray as $v){
  270.   if($shownon==1&&empty($v)) $v= "noValue";
  271.   echo $sprt1.$v.$sprt2;
  272. }
  273. echo $sprt4;
  274.   }
  275.   function location($field,$keyValue){
  276.     $field=$this->chkField($field);
  277. if ($field==-1) return false;
  278. for($i=$this->curLine;$i<=$this->innerCount;$i++){
  279.   if($this->curArray[$field]==$keyValue){
  280.    return true;
  281.   }
  282.   $this->next();
  283. }
  284. return false;
  285.   }

  286.   function del($recNo=-1){
  287. if($this->curLine==0) return false;
  288. $vartype= gettype($recNo);
  289. if($vartype!="integer"){
  290.   echo "del error:check ur para type.";
  291.   return false;
  292. }
  293.     if ($recNo==-1){
  294.   $recNo=$this->curLine;}
  295. elseif ($recNo>$this->innerCount||$recNo<1){
  296.   echo "del error:out over the rang.";
  297.   return false;
  298. }
  299. if (!$this->chkDel($recNo)){
  300.   if(isset($this->stringDel)){
  301.    $this->stringDel.=(\',\'.$recNo);
  302.   }else $this->stringDel = (string) $recNo;
  303. }else return false;
  304.   }
  305.   
  306.   function append($fields=""){
  307. $this->saveModify();
  308. for($i=1;$i<=$this->innerFieldsCount;$i++)
  309.   $newRec[] = "";
  310.     if(!empty($fields)){
  311.   foreach($fields as $k=>$v){
  312.    $k= $this->chkField($k);
  313.    if ($k==-1){
  314.     return false;
  315.    }
  316.             $newRec[$k]= $v;
  317.   }
  318. }
  319. $this->innerCount++;
  320. $this->curLine = $this->innerCount;
  321. $this->innerBof = false;
  322. $this->innerEof = true;
  323. unset($this->curArray);
  324. $this->curArray = &$newRec;
  325. $this->isModify = true;
  326.   }


  327. //保存修改
  328. function saveModify(){
  329.   if($this->isModify){
  330.    $this->innerRecorders[$this->curLine]= implode($this->sprt2,$this->curArray);
  331.    $this->isModify = false;
  332.    $this->fileModify= true;
  333.   }
  334. }
  335. //当指针发生变化时,初始化当前记录数组
  336. function initRec(){
  337.   $this->curArray = explode($this->sprt2,$this->innerRecorders[$this->curLine]);
  338.   if (count($this->curArray)!=$this->innerFieldsCount){
  339.    echo "The Current Recorder fields count unequal to Table\'s.\\n File will close.";
  340.    $this->close();
  341.    return false;
  342.   }
  343.   return true;
  344. }

  345. //输出当前记录信息,设计为调试用
  346. function ddisplay(){
  347.   if ($this->innerCount==0) return false;
  348.   foreach($this->innerFields as $v) echo $v."----";
  349.   echo "<br>";
  350.   foreach($this->curArray as $v) echo $v."---";
  351. }
  352. //检查记录是否已被删除
  353. function chkDel($key){
  354.   if (empty($key)&&$key!=0){
  355.    echo "the key not appoint.";
  356.    return false;
  357.   }
  358.   if (!isset($this->stringDel)){
  359.    return false;
  360.   }
  361.   if (ereg("(^|,)".$key."(,|$)",$this->stringDel)){
  362.    return true;
  363.   }
  364.   return false;
  365. }
  366. //检查提交的字段名是否合法.
  367. function chkField($field){
  368.   if (empty($field)&&($field!=0)){
  369.    echo "the field not appoint.";
  370.    return -1;
  371.   }
  372.   $vartype = gettype($field);
  373.   switch ($vartype) {
  374.    case "integer":
  375.     if ($field>=$this->innerFieldsCount){
  376.         echo "the field is large than fieldscount";
  377.         return -1;
  378.     }elseif($field<0){
  379.      echo "the field is less than 0";
  380.      return -1;
  381.     }
  382.     return $field;
  383.   case "string":
  384.    foreach ($this->innerFields as $k=>$v) if ($field==$v) return $k;
  385.    echo "the field name not found.";
  386.    return -1;
  387.   default:
  388.    echo "the field is invalid.";
  389.    return -1;
  390.   }
  391. }
  392. }
  393. ?>
复制代码






上一篇:PHPWind 首页九格支持6月13日PW6.3正式版版本
下一篇:PHP 最快PR查询代码
authicon 暗夜的烟火 发表于 2011-6-20 02:00:01 | 显示全部楼层
看看  好像不错
authicon №小乖 发表于 2011-6-20 23:59:52 | 显示全部楼层
这个不错呀
authicon haidideyu 发表于 2011-6-22 08:00:02 | 显示全部楼层
支持!好东西,拿走了!
authicon summmer 发表于 2011-6-22 23:00:03 | 显示全部楼层
好东东下下来看看
authicon lanyuqing 发表于 2011-6-25 22:00:01 | 显示全部楼层
很好的,我喜欢
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-6 05:46

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表