让DEDECMSv4也可使用v5的sql标签

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;
 }

如下图所示:

大小: 14.13 K
尺寸: 362 x 500
浏览: 148 次
点击打开新窗口浏览全图

再在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())
     );
    }

如下图所示:

大小: 5.27 K
尺寸: 500 x 187
浏览: 130 次
点击打开新窗口浏览全图

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())
     );
     }

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

下面是我测试V4同时用loop和sql调用文章的例子。

模板:

大小: 7.36 K
尺寸: 478 x 270
浏览: 219 次
点击打开新窗口浏览全图

前台:

大小: 8.84 K
尺寸: 422 x 270
浏览: 165 次
点击打开新窗口浏览全图

修改的5个文件打包下载:dedecmsv4sql.rar

 

Trackbacks: 点击获得Trackback地址,Encode: UTF-8  点击获得Trackback地址,Encode: GB2312 or GBK  点击获得Trackback地址,Encode: BIG5
分类:DedeCms
浏览概况:评论:0 | 阅读:9540
文章评论(0)
发表评论
用户名:
密码(游客无需):
网址/E-mail:
      记住我的信息