织梦DEDE分类信息实现联动筛选(支持多条件多级选项)解决方案
发布时间:2017-03-25 来源:未知 浏览:404 关键词:
很多织梦建站的站长在做产品列表页的时候,产品分类多而且都是关联的,用户不能快速的找到自己需要的东西,很多情况下都需要用到筛选功能,织梦后台列表页默认是没有联动筛选功能,实现这个联动筛选功能需要对织梦进行二次开发,接下来教大家如何实现这个联动筛选功能,如下图所示:
注:因为织梦默认是禁止使用php标签,所以需要在后台模板引擎禁用标签里面解除这个标签的禁用,具体方法:
后台——系统——系统基本参数——其它选项——模板引擎禁用标签:php
将这个PHP去掉后保存就可以了!
第一步:打开arc.listview.class.php 找到:if(empty($cfg_need_typeid2)) $cfg_need_typeid2 = 'N';
在下面加入以下代码:
在下面加入以下代码:
//联动筛选获得附加表的相关信息
$addtable = $this->ChannelUnit->ChannelInfos['addtable'];
if($addtable!="")
{
$addJoin = " LEFT JOIN `$addtable` ON arc.id = ".$addtable.'.aid ';
$addField = '';
$fields = explode(',',$this->ChannelUnit->ChannelInfos['listfields']);
foreach($fields as $k=>$v)
{
$nfields[$v] = $k;
}
if(is_array($this->ChannelUnit->ChannelFields) && !empty($this->ChannelUnit->ChannelFields))
{
foreach($this->ChannelUnit->ChannelFields as $k=>$arr)
{
if(isset($nfields[$k]))
{
if(!empty($arr['rename'])) {
$addField .= ','.$addtable.'.'.$k.' as '.$arr['rename'];
}
else {
$addField .= ','.$addtable.'.'.$k;
}
}
}
}
if (isset($_REQUEST['tid']))
{
foreach($_GET as $key => $value) {
$filtersql .= ($key!="tid" && $key!="TotalResult" &&
$key!="PageNo") ? " AND $addtable.".wwwcms_filter($key)." =
'".wwwcms_filter(urldecode($value))."'" : '';
}
}
}
else
{
$addField = '';
$addJoin = '';
}
第二步:打开extend.func.php文件在底部加入以下代码:
第二步:打开extend.func.php文件在底部加入以下代码:
/*联动筛选字符过滤函数*/
function wwwcms_filter($str,$stype="inject") {
if ($stype=="inject") {
$str = str_replace(
array( "select", "insert", "update", "delete", "alter",
"cas", "union", "into", "load_file", "outfile", "create", "join",
"where", "like", "drop", "modify", "rename", "'", "/*", "*", "../",
"./"),
array("","","","","","","","","","","","","","","","","","","","","",""),
$str);
} else if ($stype=="xss") {
$farr = array("/\s+/" ,
"/<(\/?)(script|META|STYLE|HTML|HEAD|BODY|STYLE
|i?frame|b|strong|style|html|img|P|o:p|iframe|u
|em|strike|BR|div|a|TABLE|TBODY|object|tr|td
|st1:chsdate|FONT|span|MARQUEE|body|title |\r\n|link
|meta|\?|\%)([^>]*?)>/isU",
"/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
);
$tarr = array(" ",
"",
"\\1\\2",
);
$str = preg_replace($farr, $tarr, $str);
$str = str_replace(
array( "<", ">", "'", "\"", ";", "/*", "*", "../", "./"),
array("<",">","","","","","","",""),
$str);
}
return $str;
}
/**
* 载入自定义表单(用于发布)
*
* @access public
* @param string $fieldset 字段列表
* @param string $loadtype 载入类型
* @return string
*/
function AddFilter($channelid, $type=1, $fieldsnamef, $defaulttid, $loadtype='autofield')
{
global $tid,$dsql,$id;
$tid = $defaulttid ? $defaulttid : $tid;
if ($id!="")
{
$tidsq = $dsql->GetOne(" Select typeid From `dede_archives` where id='$id' ");
$tid = $tidsq["typeid"];
}
$nofilter = (isset($_REQUEST['TotalResult']) ?
"&TotalResult=".$_REQUEST['TotalResult'] :
'').(isset($_REQUEST['PageNo']) ? "&PageNo=".$_REQUEST['PageNo'] :
'');
$filterarr = wwwcms_filter(stripos($_SERVER['REQUEST_URI'],
"list.php?tid=") ? str_replace($nofilter, '', $_SERVER['REQUEST_URI']) :
$GLOBALS['cfg_cmsurl']."/plus/list.php?tid=".$tid);
$cInfos = $dsql->GetOne(" Select * From `dede_channeltype` where id='$channelid' ");
$fieldset=$cInfos['fieldset'];
$dtp = new DedeTagParse();
$dtp->SetNameSpace('field','<','>');
$dtp->LoadSource($fieldset);
$dede_addonfields = '';
if(is_array($dtp->CTags))
{
foreach($dtp->CTags as $tid=>$ctag)
{
$fieldsname = $fieldsnamef ? explode(",", $fieldsnamef) : explode(",", $ctag->GetName());
if(($loadtype!='autofield' || ($loadtype=='autofield' &&
$ctag->GetAtt('autofield')==1)) &&
in_array($ctag->GetName(), $fieldsname) )
{
$href1 = explode($ctag->GetName().'=', $filterarr);
$href2 = explode('&', $href1[1]);
$fields_value = $href2[0];
$dede_addonfields .= '<dl><dt>'.$ctag->GetAtt('itemname').':</dt><dd>';
switch ($type) {
case 1:
$dede_addonfields .=
(preg_match("/&".$ctag->GetName()."=/is",$filterarr,$regm) ?
'<a title="全部"
href="'.str_replace("&".$ctag->GetName()."=".$fields_value,"",$filterarr).'">全部</a>'
: '<dd><a class="on">全部</a>').'';
$addonfields_items = explode(",",$ctag->GetAtt('default'));
for ($i=0; $i<count($addonfields_items); $i++)
{
$href = stripos($filterarr,$ctag->GetName().'=') ?
str_replace("=".$fields_value,"=".urlencode($addonfields_items[$i]),$filterarr)
:
$filterarr.'&'.$ctag->GetName().'='.urlencode($addonfields_items[$i]);//echo
$href;
$dede_addonfields .=
($fields_value!=urlencode($addonfields_items[$i]) ? '<a
title="'.$addonfields_items[$i].'"
href="'.$href.'">'.$addonfields_items[$i].'</a>' : '<a
class="on">'.$addonfields_items[$i].'</a>');
}
$dede_addonfields .= '</dd></dl>';
break;
case 2:
$dede_addonfields .= '<select
name="filter"'.$ctag->GetName().'
onchange="window.location=this.options[this.selectedIndex].value">
'.'<option
value="'.str_replace("&".$ctag->GetName()."=".$fields_value,"",$filterarr).'">全部</option>';
$addonfields_items = explode(",",$ctag->GetAtt('default'));
for ($i=0; $i<count($addonfields_items); $i++)
{
$href = stripos($filterarr,$ctag->GetName().'=') ?
str_replace("=".$fields_value,"=".urlencode($addonfields_items[$i]),$filterarr)
:
$filterarr.'&'.$ctag->GetName().'='.urlencode($addonfields_items[$i]);
$dede_addonfields .= '<option
value="'.$href.'"'.($fields_value==urlencode($addonfields_items[$i]) ? '
selected="selected"' :
'').'>'.$addonfields_items[$i].'</option>
';
}
$dede_addonfields .= '</select><br/>
';
break;
}
}
}
}
echo $dede_addonfields;
}
第三步:到这里PHP文件修改的部分就完成了!接下来就需要给指定的模型添加字段了,这里需要注意的就是 字段类型的选择,字段类型需要选择单选按钮或者使用select下拉框,如下图所示:
第四部: 前台调用标签
{dede:php}AddFilter(3,1,'fenleia,fenleib,fenleic');{/dede:php}
注:标签说明:
AddFilter函数里面的第一个数字3是内容模型的id号,各个内容模型的id请到后台内容模型管理里面查看;
第二个数字1是表示筛选的样式,1是使用超链接进行选择,2是使用select下拉框选择,具体可以自己设置了查看效果;
第三个部分'fenleia,fenleib,fenleic'是要添加筛选功能的字段名,这里有3个字段,注意需要用半角逗号隔开。