鸿轩电器 發表於 2024-5-10 00:00:00

织梦模板用{dede:sql}标签如何实现分页的示例代码

<p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        近研究了一下织梦CMS系统,看到一篇关于sql标签调用数据列表如何翻页的文章,感觉不错,贴出来大家分享一下。相信很多使用dedecms的朋友在网上查找关于dede:sql标签进行分页的解决方案时都不尽如人意,尤其是在列表页使用dede:sql调用外部数据(所谓调用外部数据就是指在后台只是创建个空栏目,然后对应的列表模板文件中使用dede:sql指定自定义的数据源,数据源与该栏目本身是没有逻辑关系的,目的是为了让织梦能按照它的规则来帮我们将数据源生成静态文件予以展示)时,我本人也搜索了很多资料,网上的答案都不够完美,有的是直接在模板文件中执行php代码来实现分页,显然此方法无法生成静态文件,有的直接在sql里面指定limit参数,但又无法实现智能分页,织梦官方也没有给出具体的解决方案,在dede论坛有看到织梦核心人物天涯给出的回复是采用自由列表的方法,显然自由列表无法指定外部数据源,最后实在没办法只能自己动手了,首先想到的思路是将dede:list标签进行改造了,熟悉dede的朋友应该知道这个列表页专用标签的工作原理大致是先通过栏目变量id获取到对应的数据源再呈现到页面上来,那么我们就可以让它不仅仅通过栏目变量id还可以通过指定的sql语句来获取数据源了,比如我们可以另外嵌入一个类似{dede:listsql sql=&#39;select * from wp_posts&#39; pagesize=&#39;10&#39;}的标签来使用。</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        OK,思路已经有了,接下来我们打开include/arc.listview.class.php这个文件来给它动个小手术吧!</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        找到:</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:php;toolbar:false;">if(!is_object($ctag))
{
&nbsp;$ctag&nbsp;=&nbsp;$this-&gt;dtp-&gt;GetTag(&quot;list&quot;);
}</pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        这一段,在其后添加如下代码:</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:php;toolbar:false;">if(!is_object($ctag))
{
&nbsp;$ctag&nbsp;=&nbsp;$this-&gt;dtp-&gt;GetTag(&quot;listsql&quot;);
&nbsp;if&nbsp;(is_object($ctag))
&nbsp;{
&nbsp;&nbsp;$cquery&nbsp;=&nbsp;$ctag-&gt;GetAtt(&quot;sql&quot;);
&nbsp;&nbsp;$cquery&nbsp;=&nbsp;preg_replace(&quot;/SELECT(.*?)FROM/is&quot;,&nbsp;&quot;&nbsp;SELECT&nbsp;count(*)&nbsp;as&nbsp;dd&nbsp;FROM&nbsp;&quot;,&nbsp;$cquery);
&nbsp;&nbsp;$cquery&nbsp;=&nbsp;preg_replace(&quot;/ORDER(.*?)SC/is&quot;,&nbsp;&quot;&quot;,&nbsp;$cquery);
&nbsp;&nbsp;$row&nbsp;=&nbsp;$this-&gt;dsql-&gt;GetOne($cquery);
&nbsp;&nbsp;if(is_array($row))
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;$this-&gt;TotalResult&nbsp;=&nbsp;$row[&#39;dd&#39;];
&nbsp;&nbsp;}
&nbsp;&nbsp;else
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;$this-&gt;TotalResult&nbsp;=&nbsp;0;
&nbsp;&nbsp;}
&nbsp;}
}
//end</pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        然后找到:</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:php;toolbar:false;">if($ctag-&gt;GetName()==&quot;list&quot;)
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;$limitstart&nbsp;=&nbsp;($this-&gt;PageNo-1)&nbsp;*&nbsp;$this-&gt;PageSize;
&nbsp;&nbsp;&nbsp;&nbsp;$row&nbsp;=&nbsp;$this-&gt;PageSize;
&nbsp;&nbsp;&nbsp;&nbsp;if(trim($ctag-&gt;GetInnerText())==&quot;&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$InnerText&nbsp;=&nbsp;GetSysTemplets(&quot;list_fulllist.htm&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$InnerText&nbsp;=&nbsp;trim($ctag-&gt;GetInnerText());
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;dtp-&gt;Assign($tagid,
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;GetArcList(
&nbsp;&nbsp;&nbsp;&nbsp;$limitstart,
&nbsp;&nbsp;&nbsp;&nbsp;$row,
&nbsp;&nbsp;&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;col&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;titlelen&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;infolen&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;imgwidth&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;imgheight&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;listtype&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;orderby&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;$InnerText,
&nbsp;&nbsp;&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;tablewidth&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;$ismake,
&nbsp;&nbsp;&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;orderway&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;)
&nbsp;&nbsp;&nbsp;&nbsp;);
&nbsp;&nbsp;&nbsp;}</pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        这一段,在其后添加如下代码:</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:php;toolbar:false;">else&nbsp;if($ctag-&gt;GetName()==&quot;listsql&quot;)
&nbsp;{
&nbsp;&nbsp;$limitstart&nbsp;=&nbsp;($this-&gt;PageNo-1)&nbsp;*&nbsp;$this-&gt;PageSize;
&nbsp;&nbsp;$row&nbsp;=&nbsp;$this-&gt;PageSize;
&nbsp;&nbsp;if(trim($ctag-&gt;GetInnerText())==&quot;&quot;)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;$InnerText&nbsp;=&nbsp;GetSysTemplets(&quot;list_fulllist.htm&quot;);
&nbsp;&nbsp;}
&nbsp;&nbsp;else
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;$InnerText&nbsp;=&nbsp;trim($ctag-&gt;GetInnerText());
&nbsp;&nbsp;}
&nbsp;&nbsp;$this-&gt;dtp-&gt;Assign($tagid,
&nbsp;&nbsp;$this-&gt;GetSqlList(
&nbsp;&nbsp;$limitstart,
&nbsp;&nbsp;$row,
&nbsp;&nbsp;$ctag-&gt;GetAtt(&quot;sql&quot;),
&nbsp;&nbsp;$InnerText
&nbsp;&nbsp;)
&nbsp;&nbsp;);
&nbsp;}
&nbsp;//end</pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        最后找到function GetArcList这个方法,在其后添加一个可以通过传入sql参数获取指定数据源的方法,代码如下:</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:php;toolbar:false;">/**
&nbsp;*&nbsp;通过listsql标签中sql属性传入的参数来获得一个单列的文档列表
&nbsp;*&nbsp;*/
function&nbsp;GetSqlList($limitstart&nbsp;=&nbsp;0,&nbsp;$row&nbsp;=&nbsp;10,&nbsp;$sql&nbsp;=&nbsp;&#39;&#39;,&nbsp;$innertext){
&nbsp;global&nbsp;$cfg_list_son;
&nbsp;$innertext&nbsp;=&nbsp;trim($innertext);
&nbsp;if&nbsp;($innertext&nbsp;==&nbsp;&#39;&#39;)&nbsp;{
&nbsp;&nbsp;$innertext&nbsp;=&nbsp;GetSysTemplets(&#39;list_fulllist.htm&#39;);
&nbsp;}
&nbsp;//处理SQL语句
&nbsp;$limitStr&nbsp;=&nbsp;&quot;&nbsp;LIMIT&nbsp;{$limitstart},{$row}&quot;;
&nbsp;$this-&gt;dsql-&gt;SetQuery($sql&nbsp;.&nbsp;$limitStr);
&nbsp;$this-&gt;dsql-&gt;Execute(&#39;al&#39;);
&nbsp;$t2&nbsp;=&nbsp;ExecTime();
&nbsp;//echo&nbsp;$t2-$t1;
&nbsp;$sqllist&nbsp;=&nbsp;&#39;&#39;;
&nbsp;$this-&gt;dtp2-&gt;LoadSource($innertext);
&nbsp;$GLOBALS[&#39;autoindex&#39;]&nbsp;=&nbsp;0;
&nbsp;//获取字段
&nbsp;while($row&nbsp;=&nbsp;$this-&gt;dsql-&gt;GetArray(&quot;al&quot;))&nbsp;{
&nbsp;&nbsp;$GLOBALS[&#39;autoindex&#39;]++;
&nbsp;&nbsp;if(is_array($this-&gt;dtp2-&gt;CTags))
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;foreach($this-&gt;dtp2-&gt;CTags&nbsp;as&nbsp;$k=&gt;$ctag)
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;if($ctag-&gt;GetName()==&#39;array&#39;)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//传递整个数组,在runphp模式中有特殊作用
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;dtp2-&gt;Assign($k,$row);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(isset($row[$ctag-&gt;GetName()]))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;dtp2-&gt;Assign($k,$row[$ctag-&gt;GetName()]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;dtp2-&gt;Assign($k,&#39;&#39;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&nbsp;&nbsp;$sqllist&nbsp;.=&nbsp;$this-&gt;dtp2-&gt;GetResult();
&nbsp;}//while
&nbsp;$t3&nbsp;=&nbsp;ExecTime();
&nbsp;//echo&nbsp;($t3-$t2);
&nbsp;$this-&gt;dsql-&gt;FreeResult(&#39;al&#39;);
&nbsp;return&nbsp;$sqllist;
}
//end</pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        总共就添加三段代码,每一段代码基本都参考它紧接着的上面那段原始代码,而无需改变它原来任何一个地方的代码,应该算是比较完美了,接下来在模板文件中的使用方法就跟一开始思路中所提到的那样,分页标签依旧沿用原来的,调用范例:</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:php;toolbar:false;">{dede:listsql&nbsp;sql=&#39;select&nbsp;ID,post_title&nbsp;from&nbsp;wp_posts&#39;&nbsp;pagesize=&#39;10&#39;}{/dede:listsql}
&nbsp;

&nbsp;
{dede:pagelist&nbsp;listsize=&#39;2&#39;&nbsp;listitem=&#39;index&nbsp;pre&nbsp;pageno&nbsp;next&nbsp;end&nbsp;&#39;/}</pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        注:以上解决方案适用于dedecms5.6-5.7版本。</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。</p>
頁: [1]
查看完整版本: 织梦模板用{dede:sql}标签如何实现分页的示例代码