相信不少使用dedecms的朋友在网上查找关于dede:sql标签进行分页的解决方案时都不尽如人意,尤为是在列表页使用dede:sql调用外部数据(所谓调用外部数据就是指在后台只是建立个空栏目,而后对应的列表模板文件中使用dede:sql指定自定义的数据源,数据源与该栏目自己是没有逻辑关系的,目的是为了让织梦能按照它的规则来帮咱们将数据源生成静态文件予以展现)时,我本人也搜索了不少资料,网上的答案都不够完美,有的是直接在模板文件中执行php代码来实现分页,显然此方法没法生成静态文件,有的直接在sql里面指定limit参数,但又没法实现智能分页,织梦官方也没有给出具体的解决方案,在dede论坛有看到织梦核心人物天涯给出的回复是采用自由列表的方法,显然自由列表没法指定外部数据源,最后实在没办法只能本身动手了,首先想到的思路是将dede:list标签进行改造了,熟悉dede的朋友应该知道这个列表页专用标签的工做原理大体是先经过栏目变量id获取到对应的数据源再呈现到页面上来,那么咱们就可让它不单单经过栏目变量id还能够经过指定的sql语句来获取数据源了,好比咱们能够另外嵌入一个相似{dede:listsql sql='select * from dede_feedback' pagesize='10'}的标签来使用。php
OK,思路已经有了,接下来咱们打开include/arc.listview.class.php这个文件来给它修整下吧!sql
找到:数据库
if(!is_object($ctag)) { $ctag = $this->dtp->GetTag("list"); }
这一段,在其后添加以下代码:数组
//Add by Rainyin 2012-09-26 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; } } } //End
而后找到:post
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") ) ); }
这一段,在其后添加以下代码:测试
//Add by Rainyin 2012-09-26 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 ) ); } //End
最后找到function GetArcList这个方法,在这个方法结束后面添加一个能够经过传入sql参数获取指定数据源的方法this
代码以下:code
//Add by Rainyin 2012-09-26 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; } //End
总共就添加三段代码,每一段代码基本都参考它紧接着的上面那段原始代码,而无需改变它原来任何一个地方的代码,应该算是比较完美的手术了,接下来在模板文件中的使用方法就跟一开始思路中所提到的那样,分页标签依旧沿用原来的htm
调用范例:it
// //{dede:listsql sql='select id,post_title from dede_feedback' pagesize='10'} // // [field:username /][field:dtime function="MyDate('Y-m-d',@me)"/] // [field:msg /] // //{/dede:listsql} // //{dede:pagelist listsize='2' listitem='index pre pageno next end '/} //
注:通过本人测试,以上解决方案适用于dedecms5.6和最新的dedecms v5.7版本。
支持跨数据库调用,例:
// //// // // {dede:listsql sql='select * from ecshop.ecs_comment order by add_time desc' pagesize='10'} // [field:user_name/]:[field:title/] // [field:content/]... // [field:add_time function="MyDate('Y-m-d',@me)"/] // // {/dede:listsql} // // // // {dede:pagelist listitem="info,index,end,pre,next,pageno" listsize="2"/} // // //