dedcms织梦实现⾃定义字段进⾏产品筛选(类似于购物商城参数筛选)企业⽹站中如果⼀些产品有很多不同的交叉属性,就需要⽤到:1、后台为该产品添加⼀些⾃定义的筛选字段 2、前台根据这些字段逐层筛选更⽅便客户找到他需要的产品(⽂章)本插件(⽂件)完全不动dedecms 任何核⼼⽂件;需要增加和修改的地⽅为:1、进⾏筛选的动态php页⾯;本篇在wwwroot/plus 下⾯增加 pcba.php页⾯;2、在模板⽬录下增加⽤于显⽰的模板⽂件;本篇在wwwroot/templets/default/ 下⾯增加 list_PCBA_with_type.htm 页⾯;3、在/include/⽂件价下找到 extend.func.php ⽂件增加⼀些函数本⽂仅作抛砖引⽟(功能已实现),希望⼤家⼀起来完善(美化)该插件;本⼈⽔平有限,还有很多不⾜之处;不过以下代码已经能将核⼼功能完成,也很实⽤(很多⽹友需要⽤到的)1、在wwwroot/plus 下⾯增加 pcba.php页⾯;代码如下:------------------------------------------------------------------------------------------<?phprequire_once("../ouyp/config.php");require_once(DEDEINC."/dedetag.class.php");require_once(dirname(__FILE__).'/../include/common.inc.php');require_once(DEDEINC."/extend.func.php");$currentUrl = $_SERVER["REQUEST_URI"];//根据当前URL中包含的查询字符串进⾏进⾏筛选并返回所有符合条件的板卡的ID值,以逗号分隔function returnQueryIDS($url){$data = array();$parameter = explode('&',end(explode('?',$url)));$addquery = count($parameter);foreach($parameter as $val){$tmp = explode('=',$val);$data[$tmp[0]] = $tmp[1];}$query = $query."and".$tmp[0];if($addquery == '1'){foreach ($data as $key => $value) {$value = urldecode($value);$query = "SELECT * FROM `#@__pcba` WHERE FIND_IN_SET('$value',$key)";}}else {$firstquery = array_slice($data,0,1);$otherquery = array_slice($data,1);foreach ($firstquery as $key => $value) {$value = urldecode($value);$query = "SELECT * FROM `#@__pcba` WHERE FIND_IN_SET('$value',$key)";}foreach ($otherquery as $key => $value) {$value = urldecode($value);$query = $query." and FIND_IN_SET('$value',$key)";}}$orderby = ' order by aid desc ';$query = $query.$orderby;if($addquery !== 0){global $dsql;$array = array();$dsql->SetQuery($query);$dsql->Execute();while($row = $dsql->GetArray()){$aids = array_push($array,$row['aid']);}$aids = join(",",$array);}else{$aids = 0;}return $aids;}//根据⽹址查询的字段返回所有符合条件的板卡ID值集合,并输出ID值集合中所有的产品returnPCBAArc(returnQueryIDS($currentUrl));require_once(DEDETEMPLATE.'/default/list_PCBA_with_type.htm');-------------------------------------------------------------------------------------------2、在wwwroot/templets/default/ 下⾯增加 list_PCBA_with_type.htm 页⾯核⼼显⽰代码如下:<div style="width:750px; float:left; padding-left:10px; height:150px; padding-top:7px; text-align:left; clear:both"> <table width="99%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" width="120px">板卡架构:</td><td valign="top"><?php returnFieldValuesPHP($currentUrl,"jiagou","1"); ?></td></tr><tr><td valign="top"> CPU 核⼼:</td><td valign="top"> <?php returnFieldValuesPHP($currentUrl,"hexin","1"); ?></td></tr><tr><td valign="top">应⽤领域</td><td valign="top"><?php returnFieldValuesPHP($currentUrl,"hangye","1"); ?></td></tr><tr><td valign="top">板卡尺⼨</td><td valign="top"><?php returnFieldValuesPHP($currentUrl,"type_size","1"); ?></td></tr><tr><td valign="top"> </td><td valign="top"> </td></tr></table></div>-----------------------------------------------------------------------------------------3、在/include/⽂件价下找到 extend.func.php ⽂件增加以下函数------------------------------------------------------------//根据传递过来的多个以逗号拼接的产品ID,形成数组、查询并返回相应的产品function returnPCBAArc($qstr,$orderby='id',$desc ='desc'){global $dsql;if(!empty($qstr)){$arcids = preg_replace("#[^0-9,]#", '', preg_replace("#,#", ',', $qstr));$query = "SELECT * FROM `#@__archives` WHERE id in($arcids) order by $orderby $desc";$dsql->SetQuery($query);$dsql->Execute();while($row = $dsql->GetArray()){$url = GetOneArchiveUrl($row['id']);$aTitle = $row['title'];$picURL = $row['litpic'];$gonghao = $row['gonghao'];$size = $row['size'];$gaisu = $row['gaisu'];$keydesc = $row['keydesc'];$msg .="<DIV style='float:left; border-right:1px #d0d9e2 solid; border-bottom:1px #d0d9e2 solid; width:332.6px;' >";$msg .="<UL class="productline_2"><LI><DIV class="font14" style=overflow:hidden><SPAN style='FONT-WEIGHT: bold'id=BrandList_ctl06_lblSeriesName><a href=$url class='title' title='$aTitle'>$aTitle</a></SPAN> </DIV><DIV class="pic"><a href=$url> <img class='lazy' style='BORDER:none;WIDTH: 150px;' data-original=$picURL src='/templets/default//js/grey.gif' /> </a></DIV>";$msg .="<DIV style='HEIGHT: 90px; OVERFLOW: hidden' class="intro"><SPAN id=BrandList_ctl06_lblSeriesDescription><P> <STRONG>$keydesc </STRONG><BR>$gaisu</P></SPAN></DIV><SPAN class="text_o"><a href=$url class="title">了解详情></A> </SPAN><div class="sc_size"><div style=width: 80px class="left">功耗及尺⼨:</div><span onmouseout=$(this).removeClass('size_a'); onmouseover=$(this).addClass('size_a'); class="size">$gonghao W </span> <span class="size">$size mm</span> </div></LI></UL></DIV>";}}print($msg);}//将数组转化为查询筛选字符串,类似&a=111&b=222function array_implode($glue, $separator, $array) {if ( ! is_array( $array ) ) return $array;$string = array();foreach ( $array as $key => $val ) {if ( is_array( $val ) )$val = implode( ',', $val );$string[] = "{$key}{$glue}{$val}";}return implode( $separator, $string );}-----------------------------------------------------------------------第三个函数最为关键//获取⾃定义字段所有值并附带链接function returnFieldValuesPHP($currentURl,$customField,$type='',$id='17'){global $dsql;if(!empty($customField)){$fname = $customField;$row = $dsql->GetOne("SELECT fieldset,'' as maintable,addtable,issystem FROM `#@__channeltype` WHERE id='$id'");$fieldset = $row['fieldset'];$trueTable = $row['addtable'];$dtp = new DedeTagParse();$dtp->SetNameSpace("field", "<", ">");$dtp->LoadSource($fieldset);foreach($dtp->CTags as $ctag){if(strtolower($ctag->GetName())==strtolower($fname)) break;}//将当前路径数组化$parameter = explode('&',end(explode('?',$currentURl)));$addquery = count($parameter);foreach($parameter as $val){$tmp = explode('=',$val);$data[$tmp[0]] = $tmp[1];}$rowValue = explode(",",$ctag->GetAtt('default'));$filename = "pcba.php";foreach($rowValue as $key=>$value){if($addquery !== 0){//检查当前路径中是否已经存在某个查询的⾃定义字段选项,如存在则重新赋值,否则直接⽤"&"添加查询字段if(array_key_exists($customField, $data)){$data[$customField] = $value;//将路径数组重新⽣成路径$newdata = array();$newdata = $data;$firstquery = array_slice($newdata,0,1);$otherquery = array_slice($newdata,1,5);foreach($firstquery as $k1 =>$v1){$fURl = $filename."?".$k1."=".$v1;}if(count($otherquery) !== 0){next(implode("&", $otherquery));$query = array_implode( '=', '&', $otherquery);//print_r($query);$newURl = $fURl."&".$query ;}else{$newURl = $fURl;}}else{$newURl = $currentURl."&".$customField."=".$value;}}else{$newURl = $filename."?".$customField=$value;}$va = "<a href=".$newURl.">".$value."</a> ";echo $va;}}}4、在后台增加相应的⾃定义字段,本⽰例增加的字段为:jiagou,hexin,hangye,type_size ,均为checkBox多选类型思路:1、pcba.php 页⾯作⽤:根据地址栏URL⾥⾯的筛选字段及值,找到所有符合条件的产品2、extend.func.php ⾥⾯增加的 3个扩展函数⽤于⽐对当前URL⾥⾯筛选的条件(字段)的,如⽤户要筛选的字段不在URL⾥⾯则增加、如⽤户要筛选的字段在URL⾥⾯已存在则替换字段的值,并显⽰在页⾯;。