• 全国400电话网上服务平台
    强大号码库资源任选,方便,快捷,快速开通。
    咨询热线:400-1100-266  

    织梦模板用{dede:sql}标签如何实现分页
    POST TIME:2021-05-23 00:47
    近研究了一下织梦CMS系统,看到一篇关于sql标签调用数据列表如何翻页的文章,感觉不错,贴出来大家分享一下。相信很多使用dedecms的朋友在网上查找关于dede:sql标签进行分页的解决方案时都不尽如人意,尤其是在列表页使用dede:sql调用外部数据(所谓调用外部数据就是指在后台只是创建个空栏目,然后对应的列表模板文件中使用dede:sql指定自定义的数据源,数据源与该栏目本身是没有逻辑关系的,目的是为了让织梦能按照它的规则来帮我们将数据源生成静态文件予以展示)时,我本人也搜索了很多资料,网上的答案都不够完美,有的是直接在模板文件中执行php代码来实现分页,显然此方法无法生成静态文件,有的直接在sql里面指定limit参数,但又无法实现智能分页,织梦官方也没有给出具体的解决方案,在dede论坛有看到织梦核心人物天涯给出的回复是采用自由列表的方法,显然自由列表无法指定外部数据源,最后实在没办法只能自己动手了,首先想到的思路是将dede:list标签进行改造了,熟悉dede的朋友应该知道这个列表页专用标签的工作原理大致是先通过栏目变量id获取到对应的数据源再呈现到页面上来,那么我们就可以让它不仅仅通过栏目变量id还可以通过指定的sql语句来获取数据源了,比如我们可以另外嵌入一个类似{dede:listsql sql='select * from wp_posts' pagesize='10'}的标签来使用。
     
    OK,思路已经有了,接下来我们打开include/arc.listview.class.php这个文件来给它动个小手术吧!
     
    找到:
     
    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版本。
     
     
     

    
    关于我们 | 付款方式 | 建站知识 | 增值服务 | 网站模板
    Copyright © 2003-2016
    时间:9:00-21:00 (节假日不休)
    版权所有:巨人网络(扬州)科技有限公司
    总部地址:江苏省信息产业基地11号楼四层
    《增值电信业务经营许可证》 苏B2-20120278
    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信

    微信号已复制,请打开微信添加咨询详情!