Home php教程 php手册 PHPCMS实现多重筛选实现程序

PHPCMS实现多重筛选实现程序

May 26, 2016 am 08:20 AM
explode foreach Pseudo-static

多重筛选其实对于php程序员开发者来讲是非常的简单了,下面来介绍在PHPCMS实现多重筛选实现例子,有需要了解的朋友一起来看看吧.

在我们使用PHPCMS开发一些产品展示类网站的时候,由于分类的多样化,我们需要客户在简短的时间内寻找到自己需要的产品,这样我们就会用到分类的多重筛选,也就是类似京东,淘宝等这么电商网站里面的产品筛选的功能,PHPCMS本身并没有这样的功能,需要我们进行二次开发,当然网上也出现了很多这样的功能,但不知道为什么,按照上面的步骤能实现的时候并不多,在经过一系列的查找和自己测试之后,我自己总结了以下的方法,希望后来者看到之后可以省去很多不必要的麻烦,我会尽可能的详细写出是怎么实现的。 实现的效果在这里就不贴图片了,淘宝,京东上比我做的还好。

首先,要实现这个功能,在建立字段的时候字段的类型必须选择box类型,在是否作为筛选字段的项目里勾选“是”,是否作为搜索条件勾选“是”。

这建立好字段之后,剩下的就是怎么实现这个功能了。咱们需要在PHPCMS的自定义函数库这个文件里编写相应的分类筛选函数,代码如下:

/**
 * 通过指定keyid形式显示所有联动菜单
 * @param  $keyid 菜单主id
 * @param  $linkageid  联动菜单id,0调用顶级
 * @param  $modelid 模型id
 * @param  $fieldname  字段名称
 */
function show_linkage($keyid, $linkageid = 0, $modelid = '', $fieldname='zone') {
    $datas = $infos = $array = array();
    $keyid = intval($keyid);
    $linkageid = intval($linkageid);
    //当前菜单id
    $field_value = intval($_GET[$fieldname]);
    $urlrule = structure_filters_url($fieldname,$array,1,$modelid);
    if($keyid == 0) return false;
    $datas = getcache($keyid,'linkage');
    $infos = $datas['data'];
    foreach($infos as $k=>$v){
        if($v['parentid']==$field_value){
            $array[$k]['name'] = $v['name'];
            $array[$k]['value'] = $k;
            $array[$k]['url'] = str_replace('{$'.$fieldname.'}',$k,$urlrule);
            $array[$k][&#39;menu&#39;] = $field_value == $k ? &#39;<a class="click" style="color:#fff;">&#39;.$v[&#39;name&#39;].&#39;</a>&#39; : &#39;<a href=&#39;.$array[$k][&#39;url&#39;].&#39;>&#39;.$v[&#39;name&#39;].&#39;</a>&#39; ;
        }
    }
    return $array;
}
 function structure_filters_url($fieldname,$array=array(),$type = 1,$modelid) {
    if(empty($array)) {
        $array = $_GET;
    } else {
        $array = array_merge($_GET,$array);
    }
    //TODO
    $fields = getcache(&#39;model_field_&#39;.$modelid,&#39;model&#39;);
    if(is_array($fields) && !empty($fields)) {
        ksort($fields);
        foreach ($fields as $_v=>$_k) {
            if($_k[&#39;filtertype&#39;] || $_k[&#39;rangetype&#39;]) {
                if(strpos(URLRULE,&#39;.html&#39;) === FALSE) $urlpars .= &#39;&&#39;.$_v.&#39;={$&#39;.$_v.&#39;}&#39;;
                else $urlpars .= &#39;-{$&#39;.$_v.&#39;}&#39;;
            }
        }
    }
    //后期增加伪静态等其他url规则管理,apache伪静态支持9个参数
    if(strpos(URLRULE,&#39;.html&#39;) === FALSE) $urlrule =APP_PATH.&#39;index.php?m=content&c=index&a=lists&catid={$catid}&#39;.$urlpars.&#39;&page={$page}&#39; ;
    else $urlrule =APP_PATH.&#39;list-{$catid}&#39;.$urlpars.&#39;-{$page}.html#wrap&#39;;
    //根据get传值构造URL
    if (is_array($array)) foreach ($array as $_k=>$_v) {
        if($_k==&#39;page&#39;) $_v=1;
        if($type == 1) if($_k==$fieldname) continue;
        $_findme[] = &#39;/{\$&#39;.$_k.&#39;}/&#39;;
        $_replaceme[] = $_v;
    }
     //type 模式的时候,构造排除该字段名称的正则
    if($type==1) $filter = &#39;(?!&#39;.$fieldname.&#39;.)&#39;;
    $_findme[] = &#39;/{\$&#39;.$filter.&#39;([a-z0-9_]+)}/&#39;;
    $_replaceme[] = &#39;0&#39;;        
    $urlrule = preg_replace($_findme, $_replaceme, $urlrule);   
    return  $urlrule;
}
/**
 * 生成分类信息中的筛选菜单
 * @param $field   字段名称
 * @param $modelid  模型ID
 */
function filters($field,$modelid,$diyarr = array()) {
    $fields = getcache(&#39;model_field_&#39;.$modelid,&#39;model&#39;);
    $options = empty($diyarr) ?  explode("\n",$fields[$field][&#39;options&#39;]) : $diyarr;
    $field_value = intval($_GET[$field]);
    foreach($options as $_k) {
        $v = explode("|",$_k);
        $k = trim($v[1]);
        $option[$k][&#39;name&#39;] = $v[0];
        $option[$k][&#39;value&#39;] = $k;
        $option[$k][&#39;url&#39;] = structure_filters_url($field,array($field=>$k),2,$modelid);
        $option[$k][&#39;menu&#39;] = $field_value == $k ? &#39;<a class="click" style="color:#fff;">&#39;.$v[0].&#39;</a>&#39; : &#39;<a href=&#39;.$option[$k][&#39;url&#39;].&#39;>&#39;.$v[0].&#39;</a>&#39; ; //这个地方的a标签的class可以改成符合你自己网站的样式,
    }
    $all[&#39;name&#39;] = &#39;全部&#39;;//&#39;全部&#39;;
    $all[&#39;url&#39;] = structure_filters_url($field,array($field=>&#39;0&#39;),2,$modelid);
    $all[&#39;menu&#39;] = $field_value == &#39;&#39; ? &#39;<a class="click" style="color:#fff;">&#39;.$all[&#39;name&#39;].&#39;</a>&#39; : &#39;<a href=&#39;.$all[&#39;url&#39;].&#39;>&#39;.$all[&#39;name&#39;].&#39;</a>&#39;;
    array_push($option,$all);   
    return $option;
}
Copy after login

把上面的这段代码复制到:extention.func.php这个文件里,在前台调用的时候需要一点点的改动,调用代码如下:

{php $sql = structure_filters_sql($modelid);}
{pc:content action="lists" where="$sql" catid="$catid" modelid="$modelid" num="12" page="$page"}
Copy after login

再调用的时候一定不要忽略了catid和modelid,二者缺其一无法调用。产品分类怎么调用呢?相信大家也看到了,在刚刚添加的函数里面有一个fliters函数,我们使用这个函数来调用已添加的box类型的字段,读取出我们需要进行筛选的分类。代码如下:

{loop filters(&#39;fengge&#39;,$modelid) $r} //filters(&#39;需要调用的字段&#39;,&#39;字段所在的模型ID&#39;)
<li>{$r[menu]}</li>
{/loop}
Copy after login

好了,在进行了如上几个步骤之后,你就会发现前台的分类筛选功能已经可以使用了,这时你是否是发现URL太长太不美观?没关系,假如你的空间支持伪静态的话,就可以把网站的URL调的简单美观。PHPCMS安装包里自带了一个.htaccess文件,直接复制到根目录里就好了。如果是IIS服务器的话,需要httpd.ini文件,转化一下就可以了,现在的IIS7有一个导入规则的功能,可以直接把.htaccess文件转化成web.config文件,这样就实现了伪静态。一定要注意,把你自己添加的box筛选字段也加入到伪静态的规则里,否则是出现一些不必要的错误。


教程链接:

随意转载~但请保留教程地址★

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

​The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

Let's talk about how to use pseudo-static to hide the php suffix Let's talk about how to use pseudo-static to hide the php suffix Mar 20, 2023 pm 06:46 PM

Pseudo-static refers to the technology of accessing dynamic URL addresses by disguising them as static addresses, while hiding the PHP suffix is ​​to modify the server configuration so that the PHP suffix is ​​no longer displayed when accessing dynamic pages. The advantage of this is that it can enhance the security of the website and avoid being cracked, while also making it more beautiful and increasing the user experience. This article will introduce in detail how to use pseudo-static to hide the php suffix to improve the security and user experience of the website.

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ​​in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ​​swapped. $original_array=[

Optimizing website SEO: practice of pseudo-static hiding php suffix Optimizing website SEO: practice of pseudo-static hiding php suffix Mar 07, 2024 pm 12:27 PM

As we all know, optimizing the SEO of a website is a very important part of website operation. The default URLs of dynamic web systems (such as PHP) used by many websites have extensions (.php, .html, etc.), which will affect the SEO effect of the website. In order to improve the optimization effect of the website, a common practice is to change the dynamic URL to a pseudo-static URL to hide the extension name and improve the user experience and search engine ranking of the website. This article will take "pseudo-static hidden php suffix" as the theme, introduce how to achieve this optimization in PHP websites, and

How to use PHP explode function and solve errors How to use PHP explode function and solve errors Mar 10, 2024 am 09:18 AM

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

Common errors and solutions when using the explode function in PHP Common errors and solutions when using the explode function in PHP Mar 11, 2024 am 08:33 AM

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

See all articles