找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 107|回复: 0

[cms教程] dede:sql实现分页,织梦sql实现分页的办法

[复制链接]

该用户从未签到

发表于 2020-9-24 10:47:25 | 显示全部楼层 |阅读模式

您需要 登录 才可以下载或查看,没有账号?立即注册

×
文章介绍
         
        思路是把dede:list标签进行改造, 列表页专用标签的工作原理大致是先通过栏目变量id获取到对应的数据源再呈现到页面上来,那么 就可以让它不仅仅通过栏目变量id还可以通过指定的sql语句来获取数据源 可以另外嵌入一个类似{dede:listsql sql='select * from wp_posts' pagesize='10'}的标签来使用。
         
        打开include/arc.listview.class.php这个文件
         
        找到:
         
         if ( ! is_object ( $ctag ) ) { $ctag = $this -> dtp -> GetTag ( "list" ) ; }
         
        这一段,在其后添加如下代码:
         
        if(!is_object($ctag))
        {
        $ctag = $this->dtp->GetTag("listsql");
        if (is_object($ctag))
        {
        $cquery = $ctag->GetAtt("sql");
        $cquery = preg_replace("/SELECT(.*?)FROM/is", " SELECT count(*) as dd FROM ", $cquery);
        $cquery = preg_replace("/ORDER(.*?)SC/is", "", $cquery);
        $row = $this->dsql->GetOne($cquery);
        if(is_array($row))
        {
        $this->TotalResult = $row['dd',;
        }
        else
        {
        $this->TotalResult = 0;
        }
        }
        }
        然后找到:
        if($ctag->GetName()=="list")
        {
        $limitstart = ($this->PageNo-1) * $this->PageSize;
        $row = $this->PageSize;
        if(trim($ctag->GetInnerText())=="")
        {
        $InnerText = GetSysTemplets("list_fulllist.htm");
        }
        else
        {
        $InnerText = trim($ctag->GetInnerText());
        }
        $this->dtp->Assign($tagid,
        $this->GetArcList(
        $limitstart,
        $row,
        $ctag->GetAtt("col")]
        $ctag->GetAtt("titlelen")]
        $ctag->GetAtt("infolen")]
        $ctag->GetAtt("imgwidth")]
        $ctag->GetAtt("imgheight")]
        $ctag->GetAtt("listtype")]
        $ctag->GetAtt("orderby")]
        $InnerText,
        $ctag->GetAtt("tablewidth")]
        $ismake,
        $ctag->GetAtt("orderway")
        )
        );
        }
        这一段,在其后添加如下代码:
        else if($ctag->GetName()=="listsql")
        {
        $limitstart = ($this->PageNo-1) * $this->PageSize;
        $row = $this->PageSize;
        if(trim($ctag->GetInnerText())=="")
        {
        $InnerText = GetSysTemplets("list_fulllist.htm");
        }
        else
        {
        $InnerText = trim($ctag->GetInnerText());
        }
        $this->dtp->Assign($tagid,
        $this->GetSqlList(
        $limitstart,
        $row,
        $ctag->GetAtt("sql")]
        $InnerText
        )
        );
        }
        最后找到function GetArcList这个方法,在其后添加一个可以通过传入sql参数获取指定数据源的方法,代码如下:
         
        function GetSqlList($limitstart = 0] $row = 10] $sql = '', $innertext){
         
        global $cfg_list_son;
        $innertext = trim($innertext);
         
        if ($innertext == '') {
        $innertext = GetSysTemplets('list_fulllist.htm');
        }
        //处理SQL语句
        $limitStr = " LIMIT {$limitstart},{$row}";
         
        $this->dsql->SetQuery($sql . $limitStr);
        $this->dsql->Execute('al');
        $t2 = ExecTime();
         
        //echo $t2-$t1;
        $sqllist = '';
        $this->dtp2->LoadSource($innertext);
        $GLOBALS['autoindex', = 0;
         
        //获取字段
        while($row = $this->dsql->GetArray("al")) {
         
        $GLOBALS['autoindex',++;
         
        if(is_array($this->dtp2->CTags))
        {
        foreach($this->dtp2->CTags as $k=>$ctag)
        {
        if($ctag->GetName()=='array')
        {
        //传递整个数组,在runphp模式中有特殊作用
        $this->dtp2->Assign($k,$row);
        }
        else
        {
        if(isset($row[$ctag->GetName()]))
        {
        $this->dtp2->Assign($k,$row[$ctag->GetName()]);
        }
        else
        {
        $this->dtp2->Assign($k,'');
        }
        }
        }
        }
         
        $sqllist .= $this->dtp2->GetResult();
         
        }//while
         
        $t3 = ExecTime();
        //echo ($t3-$t2);
        $this->dsql->FreeResult('al');
         
        return $sqllist;
        }
        总共就添加三段代码,调用范例:
        {dede:listsql sql='select ID,post_title from wp_posts' pagesize='10'}
        [*,[url=,[field:post_title /,[/url]
        {/dede:listsql}
       
        {dede:pagelist listsize='2' listitem='index pre pageno next end '/}
         
        {dede:sql}和{dede:listsql}的文章链接地址
回复

使用道具 举报

网站地图|页面地图|文字地图|Archiver|手机版|小黑屋|找资源 |网站地图

GMT+8, 2024-10-8 08:22

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表