/*这是文本数据库的类*/
class txtDb{
var $version="0.1.0";
var $id=0;#current line id
var $root;#the path for DataBase root
var $exten=".php";#数据表扩展名
var $tableName;#当前操作的表路径(root/tabname.exten)
var $strHeader;#当前操作的表表头(<?die()? >\r字段名\r字段名\r字段名\n)
var $recordLines;#当前记录窜数组(值\r值\r值\n,值\r值\r值\n)
var $dbValues;#当前数据表(二维数组)
var $fieldNames;#字段名数组(字段名,字段名,字段名)
var $fields;#字段数目
var $lines;#记录数
var $isopen=false;#是否有数据表打开
var $stime;#记录数据执行时间
var $start;#记录数据索引开始处id
var $sum;#记录数据索引数目
###########################################
/*
Set Db root
*/
function txtDb($root="database"){
$this->stime=array_sum(split(" ",microtime()));
$this->root=$root;
if(!is_dir($this->root))
if(!mkdir($this->root,0777)){
$msg="Datebase root can't be create!";
$this->out($msg);
}
}
/*
select()
select where column==querystring then return array
*/
function select($column,$querystring){
if((trim($querystring)=="")) return false;
if(!$this->isopen)return false;
if(!in_array(trim($coumn),$this->fieldNames))return false;
$querystring=strtolower(trim($querystring));
foreach($this->dbValues as $key=>$value){
if(strtolower($value[$column])==$querystring)
$dbValues[$key]=$value;
}
return is_array($dbValues)?$dbValues:false;
}
/*
query(string field,string querystring,int start,int sum);
query $querystring in range of $dbValues[$start->$start+$sum][$field]
Use: $db->open(...);$db->search($field,$keyword);
*/
function query($querystring,$fields){
if(!is_array($fields))return false;
$querystring=strtolower(trim($querystring));
if(($querystring=="")or(!$this->isopen)) return false;
foreach($this->dbValues as $key=>$value){
unset($bequery);
foreach($fields as $field)$bequery.=$value[$field];
if(ereg("($querystring)",strtolower($bequery)))
if(eregi("($querystring)",$bequery))
if(strpos(strtolower($bequery),strtolower($querystring))!== false)
$dbValues[$key]=$value;
}
return is_array($dbValues)?$dbValues:false;
}
/*
open("tblName")
*/
function open($tblName,$start=0,$num=0){
$this->id=0;
$this->start=$start;
$this->num=$num;
$this->tableName=$this->root."/".$tblName.$this->exten;
if(!file_exists($this->tableName)){
$this->close;
$msg="Can't open table ".$this->tableName."! maybe it not exists";
$this->out($msg);
return false;
}
$this->recordLines=file($this->tableName);
$this->fieldNames=explode("\r",$this->recordLines[0]);
$this->strHeader=$this->recordLines[0];
########################
array_pop($this->fieldNames);
########################
array_shift($this->fieldNames);//clear"<?die();? >"
array_shift($this->recordLines);//clear strHeader line
$this->fieldNames=array_unique($this->fieldNames);
$this->fields=count($this->fieldNames);
$this->lines=count($this->recordLines);
################
$this->getDbValues();
################
$this->isopen=true;
return $this->dbValues;
}
/*
shift(id)
*/
function shift($id){
//....................
}
/*
append(array("fields"=>"value","fields"=>"value"...))
*/
function append($values,$id="NULL"){
if(!$this->isopen) return false;
if($id=="NULL")$id=$this->id;
foreach($this->fieldNames as $field){
$strData.=$this->safeconvert($values[$field])."\r";
}
$this->recordLines=$this->insertToArray(array($strData."\n"),$this->recordLines,$id);
return $this->savetable();
}
/*
sortby(string fieldname,string desc|asc);
*/
function sortby($field,$flags="asc"){
if(!$this->isopen) return false;
if(!in_array($field,$this->fieldNames)) return false;
if(!is_array($this->dbValues)) return false;
$this->cmpkey = $field;
foreach($this->dbValues as $key=>$aryValues)
$arySort[$key]=$aryValues[$field];
if($flags=="desc") arsort($arySort,0);
else asort($arySort,0);
$arytmp=$this->dbValues;
unset($this->dbValues);
foreach($arySort as $key=>$strValues)
$this->dbValues[$key]=$arytmp[$key];
return $this->dbValues;
}
/*
change($record,$id="NULL");
*/
function change($values,$id="NULL"){
if(!$this->isopen) return false;
if($id=="NULL")$id=$this->id;
foreach($this->fieldNames as $field){
$strData.=$this->safeconvert($values[$field])."\r";
}
$this->recordLines[$id]=$strData."\n";
$this->savetable();
}/*
delete($id);
*/
function delete($id="NULL"){
if(!$this->isopen) return false;
if($id=="NULL")$id=$this->id;
unset($this->recordLines[$id]);
$this->savetable();
}
/*
clear();
*/
function clear(){
if(!$this->isopen) return false;
unset($this->recordLines);
$this->savetable();
}
/*
drop();
*/
function drop(){
if(!$this->isopen) return false;
if(!unlink($this->tableName)){
$msg="Can't delete ".$this->tableName."!";
$this->out();
}
$this->close();
return true;
}
/*
create("tblName",array(field,field...));
*/
function create($tblName,$fields){
if(!is_array($fields)) return false;
$fields=array_unique($fields);
$this->tableName=$this->root."/".$this->safeconvert($tblName).$this->exten;
if(!file_exists($this->tableName)){
$strData="<?die();?".">\r";
foreach($fields as $field) {
$strData.=$this->safeconvert($field)."\r";
}
$strData.="\n";
if($this->WriteToFile($strData)){
$this->open($tblName);
return true;
}
}
$msg="Can't create ".$this->tableName."! table already exists";
$this->out($msg);
$this->close();
return false;
}
/*
close
*/
function close(){
$this->id=0;
$this->isopen=false;
$this->start=0;
$this->num=1;
unset($this->tableName);
unset($this->strHeader);
unset($this->recordLines);
unset($this->dbValues);
unset($this->fieldNames);
unset($this->lines);
}
###########################################
# 指针操作函数
/*
end();
*/
function end(){
$this->id=$this->lines;
}
/*
first();
*/
function first(){
$this->id=0;
}
/*
prev
*/
function prev($id=1){
if(($this->id-$id)>=0){
$this->id-=$id;
return true;
}else return false;
}
/*
next
*/
function next($id=1){
if(($this->id+$id)<$this->lines){
$this->id+=$id;
return true;
}else return false;
}
/*
setid
*/
function setid($newid){
if($newid<=$this->lines and $newid>=0){
$this->id=$newid;
return true;
}else return false;
}
class txtDb{
var $version="0.1.0";
var $id=0;#current line id
var $root;#the path for DataBase root
var $exten=".php";#数据表扩展名
var $tableName;#当前操作的表路径(root/tabname.exten)
var $strHeader;#当前操作的表表头(<?die()? >\r字段名\r字段名\r字段名\n)
var $recordLines;#当前记录窜数组(值\r值\r值\n,值\r值\r值\n)
var $dbValues;#当前数据表(二维数组)
var $fieldNames;#字段名数组(字段名,字段名,字段名)
var $fields;#字段数目
var $lines;#记录数
var $isopen=false;#是否有数据表打开
var $stime;#记录数据执行时间
var $start;#记录数据索引开始处id
var $sum;#记录数据索引数目
###########################################
/*
Set Db root
*/
function txtDb($root="database"){
$this->stime=array_sum(split(" ",microtime()));
$this->root=$root;
if(!is_dir($this->root))
if(!mkdir($this->root,0777)){
$msg="Datebase root can't be create!";
$this->out($msg);
}
}
/*
select()
select where column==querystring then return array
*/
function select($column,$querystring){
if((trim($querystring)=="")) return false;
if(!$this->isopen)return false;
if(!in_array(trim($coumn),$this->fieldNames))return false;
$querystring=strtolower(trim($querystring));
foreach($this->dbValues as $key=>$value){
if(strtolower($value[$column])==$querystring)
$dbValues[$key]=$value;
}
return is_array($dbValues)?$dbValues:false;
}
/*
query(string field,string querystring,int start,int sum);
query $querystring in range of $dbValues[$start->$start+$sum][$field]
Use: $db->open(...);$db->search($field,$keyword);
*/
function query($querystring,$fields){
if(!is_array($fields))return false;
$querystring=strtolower(trim($querystring));
if(($querystring=="")or(!$this->isopen)) return false;
foreach($this->dbValues as $key=>$value){
unset($bequery);
foreach($fields as $field)$bequery.=$value[$field];
if(ereg("($querystring)",strtolower($bequery)))
if(eregi("($querystring)",$bequery))
if(strpos(strtolower($bequery),strtolower($querystring))!== false)
$dbValues[$key]=$value;
}
return is_array($dbValues)?$dbValues:false;
}
/*
open("tblName")
*/
function open($tblName,$start=0,$num=0){
$this->id=0;
$this->start=$start;
$this->num=$num;
$this->tableName=$this->root."/".$tblName.$this->exten;
if(!file_exists($this->tableName)){
$this->close;
$msg="Can't open table ".$this->tableName."! maybe it not exists";
$this->out($msg);
return false;
}
$this->recordLines=file($this->tableName);
$this->fieldNames=explode("\r",$this->recordLines[0]);
$this->strHeader=$this->recordLines[0];
########################
array_pop($this->fieldNames);
########################
array_shift($this->fieldNames);//clear"<?die();? >"
array_shift($this->recordLines);//clear strHeader line
$this->fieldNames=array_unique($this->fieldNames);
$this->fields=count($this->fieldNames);
$this->lines=count($this->recordLines);
################
$this->getDbValues();
################
$this->isopen=true;
return $this->dbValues;
}
/*
shift(id)
*/
function shift($id){
//....................
}
/*
append(array("fields"=>"value","fields"=>"value"...))
*/
function append($values,$id="NULL"){
if(!$this->isopen) return false;
if($id=="NULL")$id=$this->id;
foreach($this->fieldNames as $field){
$strData.=$this->safeconvert($values[$field])."\r";
}
$this->recordLines=$this->insertToArray(array($strData."\n"),$this->recordLines,$id);
return $this->savetable();
}
/*
sortby(string fieldname,string desc|asc);
*/
function sortby($field,$flags="asc"){
if(!$this->isopen) return false;
if(!in_array($field,$this->fieldNames)) return false;
if(!is_array($this->dbValues)) return false;
$this->cmpkey = $field;
foreach($this->dbValues as $key=>$aryValues)
$arySort[$key]=$aryValues[$field];
if($flags=="desc") arsort($arySort,0);
else asort($arySort,0);
$arytmp=$this->dbValues;
unset($this->dbValues);
foreach($arySort as $key=>$strValues)
$this->dbValues[$key]=$arytmp[$key];
return $this->dbValues;
}
/*
change($record,$id="NULL");
*/
function change($values,$id="NULL"){
if(!$this->isopen) return false;
if($id=="NULL")$id=$this->id;
foreach($this->fieldNames as $field){
$strData.=$this->safeconvert($values[$field])."\r";
}
$this->recordLines[$id]=$strData."\n";
$this->savetable();
}/*
delete($id);
*/
function delete($id="NULL"){
if(!$this->isopen) return false;
if($id=="NULL")$id=$this->id;
unset($this->recordLines[$id]);
$this->savetable();
}
/*
clear();
*/
function clear(){
if(!$this->isopen) return false;
unset($this->recordLines);
$this->savetable();
}
/*
drop();
*/
function drop(){
if(!$this->isopen) return false;
if(!unlink($this->tableName)){
$msg="Can't delete ".$this->tableName."!";
$this->out();
}
$this->close();
return true;
}
/*
create("tblName",array(field,field...));
*/
function create($tblName,$fields){
if(!is_array($fields)) return false;
$fields=array_unique($fields);
$this->tableName=$this->root."/".$this->safeconvert($tblName).$this->exten;
if(!file_exists($this->tableName)){
$strData="<?die();?".">\r";
foreach($fields as $field) {
$strData.=$this->safeconvert($field)."\r";
}
$strData.="\n";
if($this->WriteToFile($strData)){
$this->open($tblName);
return true;
}
}
$msg="Can't create ".$this->tableName."! table already exists";
$this->out($msg);
$this->close();
return false;
}
/*
close
*/
function close(){
$this->id=0;
$this->isopen=false;
$this->start=0;
$this->num=1;
unset($this->tableName);
unset($this->strHeader);
unset($this->recordLines);
unset($this->dbValues);
unset($this->fieldNames);
unset($this->lines);
}
###########################################
# 指针操作函数
/*
end();
*/
function end(){
$this->id=$this->lines;
}
/*
first();
*/
function first(){
$this->id=0;
}
/*
prev
*/
function prev($id=1){
if(($this->id-$id)>=0){
$this->id-=$id;
return true;
}else return false;
}
/*
next
*/
function next($id=1){
if(($this->id+$id)<$this->lines){
$this->id+=$id;
return true;
}else return false;
}
/*
setid
*/
function setid($newid){
if($newid<=$this->lines and $newid>=0){
$this->id=$newid;
return true;
}else return false;
}
解决方案 »
- PHP 删除单个数据 失效?phpmyadmin里面可以,但网页上面不行??
- 向大家请教一个问题.....用PHP或HTML有或CSS来告诉我怎么弄都可以
- monkey的二元分词,分utf编码的中文时出现乱码,请大家帮偶改改
- pHp include 和 request的问题!(include文件不执行,直接将php源码输出
- 美眉问:preg_match( "/^\{\*\?template(.+)\?\*\}/", $line, $tpl_arr ) 这句正则匹配什么?
- 提交数据无法显示!请解答!!
- 发现csdn有一个问题?
- 配置环境时这个问题怎么解决??
- discuz x3 + cas 整合问题
- while嵌套while循环,为什么只循环了外边的while
- 为何每个程序都出现相同的问题
- 怎样知道某一目录下,共有多少个1_*.jpg文件?提供些思路,大家帮帮忙!肯定给分!
$strData=$this->strHeader;
if(is_array($this->recordLines))
$strData.=join('',$this->recordLines);
$this->WriteToFile($strData);
$this->getDbValues();
}
/*
Get Db Values by Array of recorrdString;
*/
function getDbValues(){
if($this->num==0)$this->num=$this->lines;
unset($this->dbValues);
$i=$this->start;
$j=$i+$this->num;
$j=$j<$this->lines?$j:$this->lines;
for($i;$i<$j;$i++){
$aryFieldValue=explode("\r",$this->recordLines[$i]);
$l=0;
foreach($this->fieldNames as $strFieldName){
$aryFieldValues[$strFieldName]=$aryFieldValue[$l];
$l++;
}
$this->dbValues[$i]=$aryFieldValues;
}
}
/*
insertToArray($Insert,$TargetArray,$id=$this->id)
function:
Insert a Value into Array by id
if id is not set then insert to currid
*/
function insertToArray($Insert,$TargetArray,$id){
$arrTmpA=@array_slice($TargetArray,0,$id);
$arrTmpB=@array_slice($TargetArray,$id);
$TargetArray=@array_merge($arrTmpA,$Insert,$arrTmpB);
return $TargetArray;
}
/*
safe("data as string");
*/
function safeconvert($string){
$string=htmlspecialchars($string);
$string=str_replace("\n","",$string);
$string=str_replace("\r","",$string);
$string=str_replace("\t"," ",$string);
return $string;
}
/*
WriteToFile(); boolen
*/
function WriteToFile($strData){
$filenum=fopen($this->tableName,"w");
flock($filenum,LOCK_EX);
$file_data=fwrite($filenum,$strData);
fclose($filenum);
return $file_data;
}
/*
out()
*/
function out($msg){
$htm="<br><br><b style='color:red;font-size:12pt;font-family:verdana;'>TxtDb Error :</b><hr style='color:blue' size=1>";
$htm.=$msg;
$htm.="<hr style='color:blue' size=1>";
die($htm);
}
/*
times()
*/
function times(){
echo "<b>Processed Time : </b>".number_format(array_sum(split(" ",microtime()))-$this->stime,7);
}
###########################################
}# End of this class 2003-6-8 14:47 episome