服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > dedecms教程 >

dedcms织梦实现自定义字段进行产品筛选

时间:2016-02-19 15:06来源: 作者: 点击:
企业dedecms网站中如果一些产品有很多不同的交叉属性,就需要用到:1、后台为该产品添加一些自定义的筛选字段 2、前台根据这些字段逐层筛选 更方便客户找到他需要的产品(文章)
企业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;
                        }
                   
                }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里面已存在则替换字段的值,并显示在页面;(责任编辑:最模板)
顶一下
(3)
75%
踩一下
(1)
25%
------分隔线----------------------------
栏目列表
热点内容