让DEDECMSv4的loop标签具有和v5sql标签相同功能

Filed under: dedecms专栏, 实践与研究 |
Posted on

DEDECMSv5版中新增加的SQL标签用起来比较爽,在功能上要比v4的loop标签强。像有些要select count(*)的计算用loop是实现不了的,要不就写function,但是SQL标签可以实现。所以,我把SQL标签功能给加入到v4 版中。
方法如下:
修改文件共5个,全部在 /include 下:
1、在 inc_arcpart_view.php 中找到:

//----------------------------------------
//获得任意表的内容
//----------------------------------------
function GetTable($tablename="",$row=6,$sort="",$ifcase="",$InnerText=""){
 
$InnerText = trim($InnerText);
 
if($tablename==""||$InnerText=="") return "";
 
$row = AttDef($row,6);
 
if($sort!="") $sort = " order by $sort desc ";
 
if($ifcase!="") $ifcase=" where $ifcase ";
 
$revalue="";
 
$this->dsql->SetQuery("Select * From $tablename $ifcase $sort limit 0,$row");
 
$this->dsql->Execute();
 
$ctp = new DedeTagParse();
 
$ctp->SetNameSpace("field","[","]");
 
$ctp->LoadSource($InnerText);
 
while($row = $this->dsql->GetArray())
    
{
    
foreach($ctp->CTags as $tagid=>$ctag){
      
if(!empty($row[$ctag->GetName()]))
      
{ $ctp->Assign($tagid,$row[$ctag->GetName()]); }
    
}
    
$revalue .= $ctp->GetResult();
 
}
 
return $revalue;
}

这一段,在它后面加上:

//----------------------------------------
//通过任意SQL查询获得内容(从V5.1扒过来的)
//----------------------------------------
function GetSql($sql="",$InnerText=""){
 
$InnerText = trim($InnerText);
 
if($sql==""||$InnerText=="") return "";
 
$revalue = "";
 
$this->dsql->SetQuery($sql);
 
$this->dsql->Execute();
 
$ctp = new DedeTagParse();
 
$ctp->SetNameSpace("field","[","]");
 
$ctp->LoadSource($InnerText);
 
while($row = $this->dsql->GetArray())
    
{
    
foreach($ctp->CTags as $tagid=>$ctag){
      
if(isset($row[$ctag->GetName()]))
      
{ $ctp->Assign($tagid,$row[$ctag->GetName()]); }
    
}
    
$revalue .= $ctp->GetResult();
 
}
 
return $revalue;
}

再在196行左右的地方找到:

else if($tagname=="loop"){
    
//数据表操作
    
$this->dtp->Assign($tagid,
      
$this->GetTable($ctag->GetAtt("table"),
      
$ctag->GetAtt("row"),$ctag->GetAtt("sort"),
      
$ctag->GetAtt("if"),$ctag->GetInnerText()
    
)
    
);
    
}

这段,在后面加上:

else if($tagname=="sql"){
    
//为V4增加的SQL标签操作
    
$this->dtp->Assign($tagid,
        
$this->GetSql($ctag->GetAtt("sql"),$ctag->GetInnerText())
    
);
    
}

2、修改下列文件,以保证SQL标签在其他模板能使用:

inc_archives_view.php
inc_arclist_view.php
inc_arcmember_view.php
inc_freelist_view.php

分别在上面的文件中找到类似下面的代码(有的可能换行或缩进等不一样):

//调用论坛主题
    
//----------------
    
else if($tagname=="loop")
    
{
      
$this->dtp->Assign($tagid,
      
$this->PartView->GetTable(
        
$ctag->GetAtt("table"),$ctag->GetAtt("row"),
        
$ctag->GetAtt("sort"),$ctag->GetAtt("if"),$ctag->GetInnerText()
      
)
      
);
    
}

在后面添加以下代码:

else if($tagname=="sql"){
    
//为V4增加的SQL标签操作
    
$this->dtp->Assign($tagid,
        
$this->PartView->GetSql($ctag->GetAtt("sql"),$ctag->GetInnerText())
    
);
    
}

大功告成!以上操作请注意{}的成对使用问题,多一个少一个都会出错。

That’s all!
原文出处:http://www.copterfly.cn/show-118-1.html

或许你还关心:


评论或留言