乐观 发表于 2015-2-16 00:27:07

PHP网页编程之Text_data 一个PHP文本数据表类

我的这套线路可能跟许多学习PHP的爱好者不谋而合,这也算是一个循序渐进的学习过程,不过新手不要看到上面的概括就以为学习蛮简单的,默默在此不得不对您稍微泼一下冷水,任何东西其实都不简单。数据   <?php
/*
This TextData Class txtTbl writen by ridge.jiang mostone@hotmail.com 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);
    $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;
}
}
}
?>


PHP于1994年由Rasmus Lerdorf创建,刚刚开始是Rasmus Lerdorf为了要维护个人网页而制作的一个简单的用Perl语言编写的程序。

老尸 发表于 2015-2-16 02:58:39

装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。

莫相离 发表于 2015-3-4 23:15:52

有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。

谁可相欹 发表于 2015-3-11 10:52:34

我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。

若相依 发表于 2015-3-11 19:17:32

再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。

分手快乐 发表于 2015-3-19 23:55:13

没接触过框架的人,也不用害怕,其实框架就是一种命名规范及插件,学会一个框架其余的框架都很好上手的。

山那边是海 发表于 2015-3-25 13:14:10

有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。

灵魂腐蚀 发表于 2015-4-1 08:16:19

至于模板嘛,各位高人一直以来就是争论不休,我一只小菜鸟就不加入战团啦,咱们新手还是多学点东西的好。

若天明 发表于 2015-4-8 17:02:53

首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。

小妖女 发表于 2015-4-18 03:56:19

基础有没有对学习php没有太大区别,关键是兴趣。

再见西城 发表于 2015-4-24 21:41:55

要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。

只想知道 发表于 2015-5-1 20:49:25

开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。

爱飞 发表于 2015-6-8 09:58:50

不禁又想起那些说php是草根语言的人,为什么认得差距这么大呢。

柔情似水 发表于 2015-6-12 18:56:37

这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。

飘飘悠悠 发表于 2015-6-19 05:11:30

曾经犯过一个很低级的错误,我在文件命名的时候用了一个横线\\\\\\\'-\\\\\\\' 号,结果找了好几个小时的错误,事实是命名的时候 是不能用横线 \\\\\\\'-\\\\\\\' 的,应该用的是下划线\\\\\\\'_\\\\\\\' ;

变相怪杰 发表于 2015-6-26 20:33:55

说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年具体的记不清啦,囧。

精灵巫婆 发表于 2015-7-12 22:29:45

个人呢觉得,配wamp 最容易漏的一步就是忘了把$PHP$目录下的libmysql.dll拷贝到windows系统目录的system32目录下,还有重启apache。

透明 发表于 2015-7-18 10:08:04

最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。

因胸联盟 发表于 2015-10-3 03:47:01

遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。
页: [1]
查看完整版本: PHP网页编程之Text_data 一个PHP文本数据表类