PHP用记事本当数据库 (初学类,高手指点)
时间:2009-05-03
来源:互联网
支持添加记录,删除记录,编辑记录,读取单条记录,读取全部记录,搜索记录(包括and 和 or),搜索时不支持and 和 or同时使用的....
初学php不久,请高手指点~~~
[code]<?php
class gettxt {
var $data="";
var $content="";
function add() {
$fo = fopen($this->data,"a+");
fwrite($fo,$this->content."\n");
fclose($fo);
} // end function add()
function del($id) {
$data = file_get_contents($this->data);
$fo = fopen($this->data,"w+");
$newdata="";
$lines = explode("\n",$data);
foreach( $lines as $line ){
if (!empty($line)) {
$tmp = explode("||",$line);
if($tmp[0] == $id) {
} else {
$newdata.=$line."\n";
}
}
}
fclose($fo);
$fo = fopen($this->data,"w+");
fwrite($fo,$newdata);
fclose($fo);
} // end function del()
function edit($id) {
$data = file_get_contents($this->data);
$fo = fopen($this->data,"w+");
$newdata="";
$lines = explode("\n",$data);
foreach( $lines as $line ){
if (!empty($line)) {
$tmp = explode("||",$line);
if($tmp[0] == $id) {
$newdata.=$this->content."\n";
} else {
$newdata.=$line."\n";
}
}
}
fclose($fo);
$fo = fopen($this->data,"w+");
fwrite($fo,$newdata);
fclose($fo);
} // end function edit()
function read($id="") {
$data = file_get_contents($this->data);
$lines = explode("\n",$data);
if (empty($id)) {
return $lines;
} else {
foreach( $lines as $line ){
$tmp = explode("||",$line);
if($tmp[0] == $id) {
return $line;
}
}
}
} // end function read()
function search1($where="") {
if (!is_array($where)) {
return $this->read();
} else {
$data = file_get_contents($this->data);
$lines = explode("\n",$data);
$tmpdata="";
foreach( $lines as $line ){
$tmp = explode("||",$line);
$tmpval_=1;
for ($i=0;$i<count($where);$i++) {
$tmpval = explode("#",$where[$i]);
if (count($tmpval)>1) {
$val1= $tmpval[0];
$val2= $tmpval[1];
if (!strpos(" ".$tmp[$val1],$val2)) {
$tmpval_=0;
break;
}
}
}
if ($tmpval_==1) {
$tmpdata.=$line."\n";
}
}
$tmpdata_=explode("\n",$tmpdata);
return $tmpdata_;
}
} // end function search1()
function search2($where="") {
if (!is_array($where)) {
return $this->read();
} else {
$data = file_get_contents($this->data);
$lines = explode("\n",$data);
$tmpdata="";
foreach( $lines as $line ){
$tmp = explode("||",$line);
$tmpval_=0;
for ($i=0;$i<count($where);$i++) {
$tmpval = explode("#",$where[$i]);
if (count($tmpval)>1) {
$val1= $tmpval[0];
$val2= $tmpval[1];
if (strpos(" ".$tmp[$val1],$val2)) {
$tmpval_=1;
break;
}
}
}
if ($tmpval_==1) {
$tmpdata.=$line."\n";
}
}
$tmpdata_=explode("\n",$tmpdata);
return $tmpdata_;
}
} // end function search2()
} // end class gettxt()
$gettxt=new gettxt();
$gettxt->data="data/data.txt";
//================添加
//$gettxt->content="19||title||posttime||<p>fsfdsfdsdjlkfdsjklfds</p>";
//$gettxt->add();
//================删除
//$gettxt->del(14);
//================编辑
//$gettxt->content="19||title11111||postti11111me1||<p>1fsfdsfds1111111djlkfdsjklfds</p>";
//$gettxt->edit(19);
//================读取单条记录
//$content=$gettxt->read(19);
//echo $content;
//================读取全部记录
//$content=$gettxt->read();
//foreach($content as $val) {
// echo $val."<br>";
//}
//================搜索记录
//搜索内容设置成一个数组,"字段的位置(从0开始)#搜索的内容"
//========多条件同时满足(AND)
//$s=array("2#1me1","3#sdj");
//$content=$gettxt->search1($s);
//foreach($content as $val) {
// echo $val."<br>";
//}
//=========多条件满足一个(OR)
//$s=array("2#pos1","2#1me1");
//$content=$gettxt->search2($s);
//foreach($content as $val) {
// echo $val."<br>";
//}
?>[/code]
数据库格式,ID推荐用时间戳....
15||title||pos1ttime||<p>fsfdsfdsdjlkfdsjklfds</p>
16||title||posttime||<p>fsfdsfdsdjlkfdsjklfds</p>
17||title||posttime||<p>fsfdsfdsdjlkfdsjklfds</p>
18||title||postti11111me1||<p>fsfdsfdsdjlkfdsjklfds</p>
19||title11111||postti11111me1||<p>1fsfdsfds1111111djlkfdsjklfds</p>
初学php不久,请高手指点~~~
[code]<?php
class gettxt {
var $data="";
var $content="";
function add() {
$fo = fopen($this->data,"a+");
fwrite($fo,$this->content."\n");
fclose($fo);
} // end function add()
function del($id) {
$data = file_get_contents($this->data);
$fo = fopen($this->data,"w+");
$newdata="";
$lines = explode("\n",$data);
foreach( $lines as $line ){
if (!empty($line)) {
$tmp = explode("||",$line);
if($tmp[0] == $id) {
} else {
$newdata.=$line."\n";
}
}
}
fclose($fo);
$fo = fopen($this->data,"w+");
fwrite($fo,$newdata);
fclose($fo);
} // end function del()
function edit($id) {
$data = file_get_contents($this->data);
$fo = fopen($this->data,"w+");
$newdata="";
$lines = explode("\n",$data);
foreach( $lines as $line ){
if (!empty($line)) {
$tmp = explode("||",$line);
if($tmp[0] == $id) {
$newdata.=$this->content."\n";
} else {
$newdata.=$line."\n";
}
}
}
fclose($fo);
$fo = fopen($this->data,"w+");
fwrite($fo,$newdata);
fclose($fo);
} // end function edit()
function read($id="") {
$data = file_get_contents($this->data);
$lines = explode("\n",$data);
if (empty($id)) {
return $lines;
} else {
foreach( $lines as $line ){
$tmp = explode("||",$line);
if($tmp[0] == $id) {
return $line;
}
}
}
} // end function read()
function search1($where="") {
if (!is_array($where)) {
return $this->read();
} else {
$data = file_get_contents($this->data);
$lines = explode("\n",$data);
$tmpdata="";
foreach( $lines as $line ){
$tmp = explode("||",$line);
$tmpval_=1;
for ($i=0;$i<count($where);$i++) {
$tmpval = explode("#",$where[$i]);
if (count($tmpval)>1) {
$val1= $tmpval[0];
$val2= $tmpval[1];
if (!strpos(" ".$tmp[$val1],$val2)) {
$tmpval_=0;
break;
}
}
}
if ($tmpval_==1) {
$tmpdata.=$line."\n";
}
}
$tmpdata_=explode("\n",$tmpdata);
return $tmpdata_;
}
} // end function search1()
function search2($where="") {
if (!is_array($where)) {
return $this->read();
} else {
$data = file_get_contents($this->data);
$lines = explode("\n",$data);
$tmpdata="";
foreach( $lines as $line ){
$tmp = explode("||",$line);
$tmpval_=0;
for ($i=0;$i<count($where);$i++) {
$tmpval = explode("#",$where[$i]);
if (count($tmpval)>1) {
$val1= $tmpval[0];
$val2= $tmpval[1];
if (strpos(" ".$tmp[$val1],$val2)) {
$tmpval_=1;
break;
}
}
}
if ($tmpval_==1) {
$tmpdata.=$line."\n";
}
}
$tmpdata_=explode("\n",$tmpdata);
return $tmpdata_;
}
} // end function search2()
} // end class gettxt()
$gettxt=new gettxt();
$gettxt->data="data/data.txt";
//================添加
//$gettxt->content="19||title||posttime||<p>fsfdsfdsdjlkfdsjklfds</p>";
//$gettxt->add();
//================删除
//$gettxt->del(14);
//================编辑
//$gettxt->content="19||title11111||postti11111me1||<p>1fsfdsfds1111111djlkfdsjklfds</p>";
//$gettxt->edit(19);
//================读取单条记录
//$content=$gettxt->read(19);
//echo $content;
//================读取全部记录
//$content=$gettxt->read();
//foreach($content as $val) {
// echo $val."<br>";
//}
//================搜索记录
//搜索内容设置成一个数组,"字段的位置(从0开始)#搜索的内容"
//========多条件同时满足(AND)
//$s=array("2#1me1","3#sdj");
//$content=$gettxt->search1($s);
//foreach($content as $val) {
// echo $val."<br>";
//}
//=========多条件满足一个(OR)
//$s=array("2#pos1","2#1me1");
//$content=$gettxt->search2($s);
//foreach($content as $val) {
// echo $val."<br>";
//}
?>[/code]
数据库格式,ID推荐用时间戳....
15||title||pos1ttime||<p>fsfdsfdsdjlkfdsjklfds</p>
16||title||posttime||<p>fsfdsfdsdjlkfdsjklfds</p>
17||title||posttime||<p>fsfdsfdsdjlkfdsjklfds</p>
18||title||postti11111me1||<p>fsfdsfdsdjlkfdsjklfds</p>
19||title11111||postti11111me1||<p>1fsfdsfds1111111djlkfdsjklfds</p>
作者: 林轻灵 发布时间: 2009-05-03

嗯……就是拿txt存储数据……
不过这样写倒是比用数据库还麻烦了……

作者: konakona 发布时间: 2009-05-03
# $data = file_get_contents($this->data);
# $lines = explode("\n",$data);
# if (empty($id)) {
# return $lines;
# } else {
# foreach( $lines as $line ){
# $tmp = explode("||",$line);
# if($tmp[0] == $id) {
# return $line;
# }
# }
# }
# } // end function read()
上面的代码明显可以使用file函数来替代,不管怎样顶下
# $lines = explode("\n",$data);
# if (empty($id)) {
# return $lines;
# } else {
# foreach( $lines as $line ){
# $tmp = explode("||",$line);
# if($tmp[0] == $id) {
# return $line;
# }
# }
# }
# } // end function read()
上面的代码明显可以使用file函数来替代,不管怎样顶下
作者: omcmc 发布时间: 2009-05-03
txt还是做做计数器什么小的程序好
作者: stjdydayou 发布时间: 2009-05-04
txt在小型应用中还是挺方便的,不过不需要模仿数据库来用,
直接先把txt全部读入,要修改和删除时,直接操作数组,改动后存进入去就行了。这样更方便
直接先把txt全部读入,要修改和删除时,直接操作数组,改动后存进入去就行了。这样更方便
作者: dmkf 发布时间: 2009-05-04
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28