企業dedecms網站中如果一些產品有很多不同的交叉屬性,就需要用到: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頁面;
代碼如下:
------------------------------------------------------------------------------------------
<?php
require_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 `waimaomoban_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 `waimaomoban_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 `waimaomoban_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='http://www.aiipc.com/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=222
function 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 `waimaomoban_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;
}