1. 程式人生 > >dedecms中實現列表頁面呼叫其它欄目的資訊

dedecms中實現列表頁面呼叫其它欄目的資訊

群裡有人問如何在dedecms中實現列表頁面呼叫不同欄目的文章資訊,以下給出解決方法,針對dedecms4.0。

首先,為dedecma增加一個標籤的屬性,我修改的標籤為【List 標記】增加屬性addonid,使用方法為:

addonid= '呼叫的欄目編號',不同的欄目請用半形“,”的分隔,這些欄目必須是最終列表欄目,同時不必在這個欄目編號中增加本欄目的編號。

例子:{dede:list pagesize='2' addonid='1,2'} {/dede:list}

繼續修改include/inc_arclist_view.php,這個比較麻煩,不會的話,直接拷貝貼上。

第一步,新增$addonid變數,如下:

class ListView
{
 var $dsql;
 var $dtp;
 var $dtp2;
 var $TypeID;
 var $TypeLink;
 var $PageNo;
 var $TotalPage;
 var $TotalResult;
 var $PageSize;
 var $ChannelUnit;
 var $ListType;
 var $Fields;
 var $PartView;
 var $StartTime;
 var $addonid; //這裡為新增的變數

……

第二步:獲得模板中的addonid的值,並且統計文章總數,修改function CountRecord()函式:

原本程式碼為:

  //------------------
  //統計列表裡的記錄
  //------------------
  function CountRecord()
  {
   global $cfg_list_son;
   //統計資料庫記錄
   $this->TotalResult = -1;
   if(isset($GLOBALS['TotalResult'])) $this->TotalResult = $GLOBALS['TotalResult'];
   if(isset($GLOBALS['PageNo'])) $this->PageNo = $GLOBALS['PageNo'];
   else $this->PageNo = 1;
   
   if($this->TotalResult==-1)
   {
     $addSql  = " arcrank > -1 ";
    
     if($cfg_list_son=='否') $addSql .= " And (typeid='".$this->TypeID."' or typeid2='".$this->TypeID."') ";
     else $addSql .= " And (".$this->TypeLink->GetSunID($this->TypeID,"#@__archives",$this->Fields['channeltype'])." Or #@__archives.typeid2='".$this->TypeID."') ";
    
     if($this->StartTime>0) $addSql .= " And senddate>'".$this->StartTime."'";
     $cquery = "Select count(*) as dd From #@__archives where $addSql";
    $row = $this->dsql->GetOne($cquery);
    if(is_array($row)) $this->TotalResult = $row['dd'];
    else $this->TotalResult = 0;
   }
   
   //初始化列表模板,並統計頁面總數
   $tempfile = $GLOBALS['cfg_basedir'].$GLOBALS['cfg_templets_dir']."/".$this->TypeLink->TypeInfos['templist'];
   $tempfile = str_replace("{tid}",$this->TypeID,$tempfile);
   $tempfile = str_replace("{cid}",$this->ChannelUnit->ChannelInfos['nid'],$tempfile);
   if(!file_exists($tempfile)){
       $tempfile = $GLOBALS['cfg_basedir'].$GLOBALS['cfg_templets_dir']."/".$GLOBALS['cfg_df_style']."/list_default.htm";
    }
   if(!file_exists($tempfile)||!is_file($tempfile)){
    echo "模板檔案:'".$tempfile."' 不存在,無法解析文件!";
    exit();
   }
   $this->dtp->LoadTemplate($tempfile);
   $ctag = $this->dtp->GetTag("page");
   if(!is_object($ctag)){ $ctag = $this->dtp->GetTag("list"); }
   if(!is_object($ctag)) $this->PageSize = 20;
   else{
     if($ctag->GetAtt("pagesize")!="") $this->PageSize = $ctag->GetAtt("pagesize");
      else $this->PageSize = 20;
    }
    $this->TotalPage = ceil($this->TotalResult/$this->PageSize);
  }

修改為:

  //------------------
  //統計列表裡的記錄
  //------------------
  function CountRecord()
  {
   global $cfg_list_son;
  
   //初始化列表模板,並統計頁面總數
   $tempfile = $GLOBALS['cfg_basedir'].$GLOBALS['cfg_templets_dir']."/".$this->TypeLink->TypeInfos['templist'];
   $tempfile = str_replace("{tid}",$this->TypeID,$tempfile);
   $tempfile = str_replace("{cid}",$this->ChannelUnit->ChannelInfos['nid'],$tempfile);
   if(!file_exists($tempfile)){
       $tempfile = $GLOBALS['cfg_basedir'].$GLOBALS['cfg_templets_dir']."/".$GLOBALS['cfg_df_style']."/list_default.htm";
    }
   if(!file_exists($tempfile)||!is_file($tempfile)){
    echo "模板檔案:'".$tempfile."' 不存在,無法解析文件!";
    exit();
   }
   $this->dtp->LoadTemplate($tempfile);
   $ctag = $this->dtp->GetTag("page");
   if(!is_object($ctag)){ $ctag = $this->dtp->GetTag("list"); }  
  if($ctag->GetAtt("addonid")!="") $this->addonid = $ctag->GetAtt("addonid");
   if(!is_object($ctag)) $this->PageSize = 20;
   else{
     if($ctag->GetAtt("pagesize")!="") {
     $this->PageSize = $ctag->GetAtt("pagesize");
   }
      else $this->PageSize = 20;
    }
 
   //統計資料庫記錄
   $this->TotalResult = -1;
   if(isset($GLOBALS['TotalResult'])) $this->TotalResult = $GLOBALS['TotalResult'];
   if(isset($GLOBALS['PageNo'])) $this->PageNo = $GLOBALS['PageNo'];
   else $this->PageNo = 1;
   
   if($this->TotalResult==-1)
   {
     $addSql  = " arcrank > -1 ";
    
  if($this->addonid!="") $isaddon = " Or #@__archives.typeid in (".$this->addonid.")";
  else $isaddon = "";
    
     if($cfg_list_son=='否') $addSql .= " And (typeid='".$this->TypeID."' or typeid2='".$this->TypeID."' ".$isaddon.") ";
     else $addSql .= " And (".$this->TypeLink->GetSunID($this->TypeID,"#@__archives",$this->Fields['channeltype'])." Or #@__archives.typeid2='".$this->TypeID."' ".$isaddon.") ";

    
     if($this->StartTime>0) $addSql .= " And senddate>'".$this->StartTime."'";
     $cquery = "Select count(*) as dd From #@__archives where $addSql";
    $row = $this->dsql->GetOne($cquery);
    if(is_array($row)) $this->TotalResult = $row['dd'];
    else $this->TotalResult = 0;
   }
 
    $this->TotalPage = ceil($this->TotalResult/$this->PageSize);
  }

說明,首先把統計資料庫記錄這部分程式碼後移,目的是為了利用獲得的屬性引數addonid,接著通過$this->addonid = $ctag->GetAtt("addonid");獲得模板中的addonid的值,然後生成新的統計資料庫的sql語句。

第三步,修改function GetArcList()函式,顯示文件列表。

原檔案為(程式碼片斷):

  if($cfg_list_son=='否') $orwhere .= " And (arc.typeid='".$this->TypeID."' or arc.typeid2='".$this->TypeID."') ";
  else $orwhere .= " And (".$this->TypeLink->GetSunID($this->TypeID,"arc",$this->Fields['channeltype'])." Or arc.typeid2='".$this->TypeID."') ";

修改為:

  if($this->addonid!="") $isaddon = " Or arc.typeid in (".$this->addonid.")";
  else $isaddon = "";
  if($cfg_list_son=='否') $orwhere .= " And (arc.typeid='".$this->TypeID."' or arc.typeid2='".$this->TypeID."' ".$isaddon.") ";
  else $orwhere .= " And (".$this->TypeLink->GetSunID($this->TypeID,"arc",$this->Fields['channeltype'])." Or arc.typeid2='".$this->TypeID."' ".$isaddon.") ";

到此就可以實現列表頁面呼叫其它欄目的資訊。有問題的話Q我。