All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
javascript代码混淆综合解决方案_JavaScript技术_编程技术
MS SQL日志文件长度过大的处理方法_[SQL Server教程]
如何让网站运营不止停留在SEO_优化技巧_seo学堂
C#教程:多线程简介
Visual C++ 6.0教程:函数中的默认参数
在ASP.NET程序中值得注意的两个地方_.net资料_编程技术
visual c++建立工具栏资源
sql2000挂起无法安装的问题_[SQL,Server教程]
Silverlight,2(beta1)数据操作(2)——使用ASP.NET,Web,Service_[Asp.Net教程]
动态网页制作技术PHP处理时间的实例_php资料_编程技术
Reading number is top 10 pictures
Sora aoi mirror memorial classics4
Sexy women in 2013--2
真正的国产-非模拍 贵在是真实2
姑娘手慢了,已经走光了
Beautiful vacuum girl1
Angie Chiu vijara myth2
史上最大的哺乳动物迁移
29 the belle stars after bath figure3
Sora aoi possession of boudoir1
Players in the eyes of a perfect love2
Download software ranking
Tram sex maniac 2 (H) rar bag11
中国结婚习俗实录
Boxer's Top ten classic battle1
Tram sex maniac 2 (H) rar bag18
Unix video tutorial2
Proficient in Eclipse
变速齿轮3.26
The Bermuda triangle1
Tram sex maniac 2 (H) rar bag9
Unix video tutorial6
归海一刀 published in(发表于) 2014/2/17 7:37:00 Edit(编辑)
PHP操作XML做数据库的类_[PHP教程]

PHP操作XML做数据库的类_[PHP教程]

PHP操作XML做数据库的类_[PHP教程]


* example 读取数据:
*
* xml = new xml("dbase.xml",'table');
*
* data=xml->xml_fetch_array();
*
* echo "

";
*
* print_r(data);
*


class xml
{
var dbase; //数据库,要读取的XML文件
var dbname; //数据库名称,顶层元素,与数据库文件名称一致
var dbtable; //数据表,要取得的节点
var parser; //剖析器
var vals; //属性
var index; //索引
var dbtable_array;//节点数组
var array; //下级节点的数组
var result; //返回的结果
var querys;


function xml(dbase,dbtable)
{
this->dbase=dbase;
this->dbname=substr(dbase,strrpos(dbase,"/")+1,-4);
this->dbtable=dbtable;
data=this->ReadXml(this->dbase);
if(!data){
die("无法读取 this->dbname.xml");
}
this->parser = xml_parser_create();
xml_parser_set_option(this->parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option(this->parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct(this->parser,data,this->vals,this->index);
xml_parser_free(this->parser);
//遍历索引,筛选出要取值的节点 节点名:dbtable
foreach (this->index as key=>val) {
if (key == this->dbtable) {
//取得节点数组
this->dbtable_array = val;
} else {
continue;
}
}
for (i=0; i < count(this->dbtable_array); i+=2) {
offset = this->dbtable_array[i] + 1;
len = this->dbtable_array[i + 1] - offset;
//array_slice() 返回根据 offset 和 length 参数所指定的 array 数组中的一段序列。
//所取节点下级数组
value=array_slice(this->vals,offset,len);
//取得有效数组,合并为结果数组
this->array[]=this->parseEFF(value);
}
return true;
}
//将XML文件读入并返回字符串
function ReadXml(file)
{
return file_get_contents(file);
}
//取得有效数组
function parseEFF(effective) {
for (i=0; i < count(effective); i++){
effect[effective[i]["tag"]] = effective[i]["value"];
}
return effect;
}
//xml_query(方法,条件,多条件时逻辑运算符and or or,插入或更新的数组)
function xml_query(method,condition,if='and',array=array())
{
if((method=='select')||(method=='count')){
return this->xml_select(method,condition,if);
} elseif(method=='insert') {
return this->xml_insert(condition,if,array);
} elseif(method=='update') {
return this->xml_update(condition,if,array);
}
}
//取得xml数组
function xml_fetch_array(condition,if)
{
//this->querys++;
row = this->array; //初始化数据数组
if(condition) {
//是否有条件,如有条件则生成符合条件的数组
//生成条件数组,条件格式 field,operator,match
condition=explode(",",condition);//条件数组
cs=count(condition)/3; //条件数
for(i=0;iconditions[]=array("field"=>condition[i*3],"operator"=>condition[i*3+1],"match"=>condition[i*3+2]);
}
//echo count(row);
for(r=0;rfor(c=0;c//i++;
condition=conditions[c]; //当前条件
field=condition['field']; //字段
operator=condition["operator"];//运算符
match=condition['match']; //匹配
if((operator=='=')&&(row[r][field]==match)){
true++;//若条件符合,符合数加1
} elseif((operator=='!=')&&(row[r][field]!=match)){
true++;//若条件符合,符合数加1
} elseif((operator=='<')&&(row[r][field]true++;//若条件符合,符合数加1
} elseif((operator=='<=')&&(row[r][field]<=match)){
true++;//若条件符合,符合数加1
} elseif((operator=='>')&&(row[r][field]>match)){
true++;//若条件符合,符合数加1
} elseif((operator=='>')&&(row[r][field]>=match)){
true++;//若条件符合,符合数加1
}
}
//根据条件取值
if(if=='and'){
//如果多条件为and,当符合数等于条件数时,生成数组
if(true==cs){
result[]=row[r];
}
} else {
//如果多条件为or,当有符合纪录时,生成数组
if(true!=0){
result[]=row[r];
}
}
//echo true;
//echo "

";
//print_r(true);
true=0;//符合条件数归零,进入下一轮循环
}
} else {
result=this->array;
}
//echo "
";
//print_r(this->result);
return result;
}
//筛选或统计
function xml_select(method,condition,if)
{
result=this->xml_fetch_array(condition,if);
if(method=='select'){
return result;
} else {
return count(result);
}


}
//插入数据
function xml_insert(condition,if,array)
{
data=this->xml_fetch_array(condition,if);//总数据数组
data[]=array; //插入后的总数据数组
this->array=data; //更新总数组
this->WriteXml(data);
}


//得到更新的XML并改写
function xml_update(condition,if,array)
{
datas=this->array; //总数据数组
subtract=this->xml_fetch_array(condition,if);//要更新的数组
//echo "

";
//print_r(data);
//print_r(datas);
//echo "每条记录中有".count(datas[0])."个值
";
for(i=0;idata=datas[i];
//echo "原始记录中的第".i."条
";
foreach(data as k=>v){
//echo "-第".i."条的".k."值为".v."
";
//echo "--要查找的数组".k."值为".subtract[0][k]."
";
if(v==subtract[0][k]){
is++;
}
}
if(is==count(data)){
//echo "----与第".i."条符合
";
datas[i]=array;
//array_splice(datas,i,i+1);
}
//echo "原始记录中的第".i."条与要查找的有".is."匹配
";
//echo "原始记录中的第".i."条结束
";
is=0;
}
//array_splice(datas,2,2+1,array);
//echo "
";
//print_r(datas);
this->array=datas;
this->WriteXml(datas);


}
//写入XML文件(全部写入)
function WriteXml(array)
{
if(!is_writeable(this->dbase)){
die("无法写入".this->dbname.".xml");
}
xml.="rn";
xml.="dbname>rn";
for(i=0;ixml.="dbtable>rn";
foreach(array[i] as k=>s){
xml.="srn";
}
xml.="
dbtable>rn";
}
xml.="
dbname>";
fp=@fopen(this->dbase,"w");
flock(fp, LOCK_EX);
rewind(fp);
fputs(fp,xml);
fclose(fp);
}
//逐行写入xml(我试着写入10000行,感觉没一次写入快,所以没用这种写入方式)
function WriteLine(array)
{
if(!is_writeable(this->dbase)){
die("无法写入".this->dbname.".xml");
}
fp=@fopen(this->dbase,"w");
rewind(fp);
flock(fp, LOCK_EX);
fputs(fp,"rn");
fputs(fp,"dbname>rn");
for(i=0;ifputs(fp,"dbtable>rn");
xml.="dbtable>rn";
foreach(array[i] as k=>s){
fputs(fp,"srn");
}
fputs(fp,"
dbtable>rn");
}
fputs(fp,"
dbname>");
fclose(fp);
}
}
?>








添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.