dedecms添加自定義標籤顯示非dede表數據
需求:調用一個商城系統裏面的產品數據到dedecms文章頁,希望對需要類似需求的人有所幫助
功能實現:爲了不用垮數據庫查詢,dedecms安裝到商城系統數據庫裏面
下面是實現的代碼(請根據自己的環境進行修改)
include/taglib/hongyungoods.lib.php
/**
* @author http://www.heui.org/
*/
if(!defined('DEDEINC')) exit('Request Error!');
functionlib_hongyungoods($ctag, $refObj)
{
global$dsql, $envs, $cfg_cmsurl;
//屬性處理
$attlist="row|6,titlelen|0,tid|0";
FillAttsDefault($ctag->CAttribute->Items,$attlist);
extract($ctag->CAttribute->Items, EXTR_SKIP);
//只在指定欄目顯示
if($tid>0 $tid!= $envs['typeid']) return'';
if( !$dsql->IsTable("sl_products") ) return'沒安裝鴻運產品模塊';
$innertext= $ctag->GetInnerText();
//@todo hongyungoods.htm
if(trim($innertext)=='') $innertext= GetSysTemplets("hongyungoods.htm");
//@todo 智能條件
$where= 1;
$limit= 5;
$ctp= newDedeTagParse();
$ctp->SetNameSpace('field', '[', ']');
$ret= '';
$query= "SELECT p.`Name` AS title,
CONCAT('http://www.hongyun2000.com/', ps.`AcStr`, '-view-', p.`ID`, '.html') AS url,
CONCAT('http://www.hongyun2000.com/uploadfile/s/', p.`Images`) AS img
FROM `sl_products` AS p
LEFT JOIN `sl_products_sort` AS ps ON p.`SID` = ps.`ID`
WHERE " . $where . "ORDER BY RAND() LIMIT " . $limit;
$dsql->Execute('me',$query);
while($rs= $dsql->GetArray('me'))
{
$rs['title'] = 0 == $titlelen?$rs['title']:cn_substr($rs['title'], $titlelen);
$ctp->LoadSource($innertext);
foreach($ctp->CTags as$tagid=>$ctag) {
if(!empty($rs[strtolower($ctag->GetName())])) {
$ctp->Assign($tagid,$rs[$ctag->GetName()]);
}
}
$ret.= $ctp->GetResult();
}
return$ret;
}
?>
調用頁面使用:
{dede:hongyungoods}
[field:title/]
{/dede:hongyungoods}