近研究了一下织梦CMS系统,看到一篇关于sql标签调用数据列表如何翻页的文章,感觉不错,贴出来大家分享一下。相信很多使用dedecms的朋友在网上查找关于dede:sql标签进行分页的解决方
1 if(!is_object($ctag))
2 {
3 $ctag = $this->dtp->GetTag("list");
4 }
这一段,在其后添加如下代码:
01 if(!is_object($ctag))
02 {
03 $ctag = $this->dtp->GetTag("listsql");
04 if (is_object($ctag))
05 {
06 $cquery = $ctag->GetAtt("sql");
07 $cquery = preg_replace("/SELECT(.*?)FROM/is", " SELECT count(*) as dd FROM ", $cquery);
08 $cquery = preg_replace("/ORDER(.*?)SC/is", "", $cquery);
09 $row = $this->dsql->GetOne($cquery);
10 if(is_array($row))
11 {
12 $this->TotalResult = $row['dd'];
13 }
14 else
15 {
16 $this->TotalResult = 0;
17 }
18 }
19 }
20 //end
然后找到:
01 if($ctag->GetName()=="list")
02 {
03 $limitstart = ($this->PageNo-1) * $this->PageSize;
04 $row = $this->PageSize;
05 if(trim($ctag->GetInnerText())=="")
06 {
07 $InnerText = GetSysTemplets("list_fulllist.htm");
08 }
09 else
10 {
11 $InnerText = trim($ctag->GetInnerText());
12 }
13 $this->dtp->Assign($tagid,
14 $this->GetArcList(
15 $limitstart,
16 $row,
17 $ctag->GetAtt("col"),
18 $ctag->GetAtt("titlelen"),
19 $ctag->GetAtt("infolen"),
20 $ctag->GetAtt("imgwidth"),
21 $ctag->GetAtt("imgheight"),
22 $ctag->GetAtt("listtype"),
23 $ctag->GetAtt("orderby"),
24 $InnerText,
25 $ctag->GetAtt("tablewidth"),
26 $ismake,
27 $ctag->GetAtt("orderway")
28 )
29 );
30 }
这一段,在其后添加如下代码:
01 else if($ctag->GetName()=="listsql")
02 {
03 $limitstart = ($this->PageNo-1) * $this->PageSize;
04 $row = $this->PageSize;
05 if(trim($ctag->GetInnerText())=="")
06 {
07 $InnerText = GetSysTemplets("list_fulllist.htm");
08 }
09 else
10 {
11 $InnerText = trim($ctag->GetInnerText());
12 }
13 $this->dtp->Assign($tagid,
14 $this->GetSqlList(
15 $limitstart,
16 $row,
17 $ctag->GetAtt("sql"),
18 $InnerText
19 )
20 );
21 }
22 //end
最后找到function GetArcList这个方法,在其后添加一个可以通过传入sql参数获取指定数据源的方法,代码如下:
01 /**
02 * 通过listsql标签中sql属性传入的参数来获得一个单列的文档列表
03 * */
04 function GetSqlList($limitstart = 0, $row = 10, $sql = '', $innertext){
05
06 global $cfg_list_son;
07 $innertext = trim($innertext);
08
09 if ($innertext == '') {
10 $innertext = GetSysTemplets('list_fulllist.htm');
11 }
12 //处理SQL语句
13 $limitStr = " LIMIT {$limitstart},{$row}";
14
15 $this->dsql->SetQuery($sql . $limitStr);
16 $this->dsql->Execute('al');
17 $t2 = ExecTime();
18
19 //echo $t2-$t1;
20 $sqllist = '';
21 $this->dtp2->LoadSource($innertext);
22 $GLOBALS['autoindex'] = 0;
23
24 //获取字段
25 while($row = $this->dsql->GetArray("al")) {
26
27 $GLOBALS['autoindex']++;
28
29 if(is_array($this->dtp2->CTags))
30 {
31 foreach($this->dtp2->CTags as $k=>$ctag)
32 {
33 if($ctag->GetName()=='array')
34 {
35 //传递整个数组,在runphp模式中有特殊作用
36 $this->dtp2->Assign($k,$row);
37 }
38 else
39 {
40 if(isset($row[$ctag->GetName()]))
41 {
42 $this->dtp2->Assign($k,$row[$ctag->GetName()]);
43 }
44 else
45 {
46 $this->dtp2->Assign($k,'');
47 }
48 }
49 }
50 }
51
52 $sqllist .= $this->dtp2->GetResult();
53
54 }//while
55
56 $t3 = ExecTime();
57 //echo ($t3-$t2);
58 $this->dsql->FreeResult('al');
59
60 return $sqllist;
61 }
62 //end
总共就添加三段代码,每一段代码基本都参考它紧接着的上面那段原始代码,而无需改变它原来任何一个地方的代码,应该算是比较完美了,接下来在模板文件中的使用方法就跟一开始思路中所提到的那样,分页标签依旧沿用原来的,调用范例:
1 {dede:listsql sql='select ID,post_title from wp_posts' pagesize='10'}
2 <li><a href="[field:ID /].html">[field:post_title /]</a></li>
3 {/dede:listsql}
4 <!--分页-->
5 {dede:pagelist listsize='2' listitem='index pre pageno next end '/}
注:以上解决方案适用于dedecms5.6-5.7版本。
