phpcms v9实现自定义多字段筛选功能联动菜单筛选下拉菜单筛选
拷贝以下代码加到到 phpcms libs functions这个目录下extention func php文件中
拷贝以下代码加到到\phpcms\libs\functions这个目录下extention.func.php文件中
- /**
- * extention.func.php 用户自定义函数库
- * @copyright (C) 2005-2010 PHPCMS
- * @license http://www.phpcms.cn/license/
- * @lastmodify 2010-10-27
- */
- /**
- * 通过指定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]['menu'] = $field_value == $k ? ''.$v['name'].'' : '.$array[$k]['url'].'>'.$v['name'].'' ;
- }
- }
- return $array;
- }
- function structure_filters_url($fieldname,$array=array(),$type = 1,$modelid) {
- if(emptyempty($array)) {
- $array = $_GET;
- } else {
- $array = array_merge($_GET,$array);
- }
- //TODO
- $fields = getcache('model_field_'.$modelid,'model');
- if(is_array($fields) && !emptyempty($fields)) {
- ksort($fields);
- foreach ($fields as $_v=>$_k) {
- if($_k['filtertype'] || $_k['rangetype']) {
- if(strpos(URLRULE,'.html') === FALSE) $urlpars .= '&'.$_v.'={$'.$_v.'}';
- else $urlpars .= '-{$'.$_v.'}';
- }
- }
- }
- //后期增加伪静态等其他url规则管理,apache伪静态支持9个参数
- if(strpos(URLRULE,'.html') === FALSE) $urlrule =APP_PATH.'index.php?m=content&c=index&a=lists&catid={$catid}'.$urlpars.'&page={$page}' ;
- else $urlrule =APP_PATH.'list-{$catid}'.$urlpars.'-{$page}.html';
- //根据get传值构造URL
- if (is_array($array)) foreach ($array as $_k=>$_v) {
- if($_k=='page') $_v=1;
- if($type == 1) if($_k==$fieldname) continue;
- $_findme[] = '/{\$'.$_k.'}/';
- $_replaceme[] = $_v;
- }
- //type 模式的时候,构造排除该字段名称的正则
- if($type==1) $filter = '(?!'.$fieldname.'.)';
- $_findme[] = '/{\$'.$filter.'([a-z0-9_]+)}/';
- $_replaceme[] = '';
- $urlrule = preg_replace($_findme, $_replaceme, $urlrule);
- return $urlrule;
- }
- /**
- * 生成分类信息中的筛选菜单
- * @param $field 字段名称
- * @param $modelid 模型ID
- */
- function filters($field,$modelid,$diyarr = array()) {
- $fields = getcache('model_field_'.$modelid,'model');
- $options = emptyempty($diyarr) ? explode("\n",$fields[$field]['options']) : $diyarr;
- $field_value = intval($_GET[$field]);
- foreach($options as $_k) {
- $v = explode("|",$_k);
- $k = trim($v[1]);
- $option[$k]['name'] = $v[0];
- $option[$k]['value'] = $k;
- $option[$k]['url'] = structure_filters_url($field,array($field=>$k),2,$modelid);
- $option[$k]['menu'] = $field_value == $k ? ''.$v[0].'' : '.$option[$k]['url'].'>'.$v[0].'' ;
- }
- $all['name'] = '全部';
- $all['url'] = structure_filters_url($field,array($field=>''),2,$modelid);
- $all['menu'] = $field_value == '' ? ''.$all['name'].'' : '.$all['url'].'>'.$all['name'].'';
- array_unshift($option,$all);
- return $option;
- }
- /**
- * 获取联动菜单层级
- * @param $keyid 联动菜单分类id
- * @param $linkageid 菜单id
- * @param $leveltype 获取类型 parentid 获取父级id child 获取时候有子栏目 arrchildid 获取子栏目数组
- */
- function get_linkage_level($keyid,$linkageid,$leveltype = 'parentid') {
- $child_arr = $childs = array();
- $leveltypes = array('parentid','child','arrchildid','arrchildinfo');
- $datas = getcache($keyid,'linkage');
- $infos = $datas['data'];
- if (in_array($leveltype, $leveltypes)) {
- if($leveltype == 'arrchildinfo') {
- $child_arr = explode(',',$infos[$linkageid]['arrchildid']);
- foreach ($child_arr as $r) {
- $childs[] = $infos[$r];
- }
- return $childs;
- } else {
- return $infos[$linkageid][$leveltype];
- }
- }
- }
- // 根据linkageid递归到父级
- function get_parent_url($modelid,$field,$linkageid=0,$array = array()){
- $modelid = intval($modelid);
- if(!$modelid || emptyempty($field)) return false;
- $fields = getcache('model_field_'.$modelid,'model');
- $keyid = $fields[$field]['linkageid'];
- $datas = getcache($keyid,'linkage');
- $infos = $datas['data'];
- if(emptyempty($linkageid)){
- $linkageid = intval($_GET[$field]);
- if(!$linkageid) return false;
- }
- $urlrule = structure_filters_url($field,array(),1,$modelid);
- $urlrule = str_replace('{$'.$field.'}',$infos[$linkageid]['parentid'],$urlrule);
- array_unshift($array,array('name'=> $infos[$linkageid]['name'],'url'=>$urlrule));
- if($infos[$linkageid]['parentid']){
- return get_parent_url($modelid,$field,$infos[$linkageid]['parentid'],$array);
- }
- return $array;
- }
- /**
- * 构造筛选时候的sql语句
- */
- function structure_filters_sql($modelid) {
- $sql = $fieldname = $min = $max = '';
- $fieldvalue = array();
- $modelid = intval($modelid);
- $model = getcache('model','commons');
- $fields = getcache('model_field_'.$modelid,'model');
- $fields_key = array_keys($fields);
- //TODO
- $sql = '`status` = \'99\'';
- foreach ($_GET as $k=>$r) {
- if(in_array($k,$fields_key) && intval($r)!=0 && ($fields[$k]['filtertype'] || $fields[$k]['rangetype'])) {
- if($fields[$k]['formtype'] == 'linkage') {
- $datas = getcache($fields[$k]['linkageid'],'linkage');
- $infos = $datas['data'];
- if($infos[$r]['arrchildid']) {
- $sql .= ' AND `'.$k.'` in('.$infos[$r]['arrchildid'].')';
- }
- } elseif($fields[$k]['rangetype']) {
- if(is_numeric($r)) {
- $sql .=" AND `$k` = '$r'";
- } else {
- $fieldvalue = explode('_',$r);
- $min = intval($fieldvalue[0]);
- $max = $fieldvalue[1] ? intval($fieldvalue[1]) : 999999;
- $sql .=" AND `$k` >= '$min' AND `$k` < '$max'";
- }
- } else {
- $sql .=" AND `$k` = '$r'";
- }
- }
- }
- return $sql;
- }
- ?>
使用方法如何:
后台添加字段(类型只能是联动菜单,和下拉列表)、然后选择作为筛选字段。
模板使用
===============================
- 您已选择:
- {loop get_parent_url($modelid,'diqu') $r}
- "{$r['url']}">{$r['name']}
- {/loop}
==============================
联动菜单使用:
- {php $zones = show_linkage(1,0,$modelid,'diqu')}
- {if !emptyempty($zones)}
- {loop $zones $r}
- {$r['menu']}
- {/loop}
- {/if}
==========================
下拉列表使用
- {loop filters('xz',$modelid) $r}
- {$r[menu]}
- {/loop}
==========================
配合列表标签使用:
- {php $sql = structure_filters_sql($modelid);}
- {pc:content action="lists" where="$sql" catid="$catid" modelid="$modelid" num="10" page="$page"}
====================================================
备注以上方法可以实现筛选功能但无法分页。您可以参考以下这篇文章来自定义分页功能。
phpcms v9自定义分页函数
http://www.juheweb.com/Tutorials/cms/phpcmsv/267.html
注意一下筛选自定义名称问题:
问题补充:
- function wz_pages($num, $curr_page, $perpage = 20, $urlrule = '', $array = array(),$setpages = 10) {
- $yt=$_GET['yt'];//筛选自定义名称
- $dq=$_GET['dq'];//筛选自定义名称
- $wz_url="&yt=$yt&dq=$dq";//筛选自定义名称
- if(defined('URLRULE') && $urlrule == '') {
- $urlrule = URLRULE;
- $array = $GLOBALS['URL_ARRAY'];
- } elseif($urlrule == '') {
- $urlrule = url_par('page={$page}');
- }
- 未完整显示,未显示的代码为复制phpcms v9分页代码
以上phpcms v9筛选无法实现分栏目显示列表,总是把整个模型的都列出不能区分栏目内容。以下解决办法
找到
- /**
- * 构造筛选时候的sql语句
- */
- function structure_filters_sql($modelid) {
- $sql = $fieldname = $min = $max = '';
- $fieldvalue = array();
- $modelid = intval($modelid);
- $model = getcache('model','commons');
- $fields = getcache('model_field_'.$modelid,'model');
- $fields_key = array_keys($fields);
- //TODO
- $sql = '`status` = \'99\'';
- foreach ($_GET as $k=>$r) {
- if(in_array($k,$fields_key) && $r!='' && ($fields[$k]['filtertype'] || $fields[$k]['rangetype'])) {
- if($fields[$k]['formtype'] == 'linkage') {
- $datas = getcache($fields[$k]['linkageid'],'linkage');
- $infos = $datas['data'];
- if($infos[$r]['arrchildid']) {
- $sql .= ' AND `'.$k.'` in('.$infos[$r]['arrchildid'].')';
- }
- } elseif($fields[$k]['rangetype']) {
- if(is_numeric($r)) {
- $sql .=" AND `$k` = '$r'";
- } else {
- $fieldvalue = explode('_',$r);
- $min = intval($fieldvalue[0]);
- $max = $fieldvalue[1] ? intval($fieldvalue[1]) : 999999;
- $sql .=" AND `$k` >= '$min' AND `$k` < '$max'";
- }
- } else {
- $sql .=" AND `$k` = '$r'";
- }
- }
- }
- return $sql;
- return $k;
- }
替换成以下代码:
- /**
- * 构造筛选时候的sql语句
- */
- function structure_filters_sql($modelid,$cityid='',$catid) { //这里加上$catid
- $sql = $fieldname = $min = $max = '';
- $fieldvalue = array();
- $modelid = intval($modelid);
- $model =getcache('model','commons');
- $fields = getcache('model_field_'.$modelid,'model');
- $fields_key = array_keys($fields); //TODO
- $sql = "`status` = 99 AND `catid`='$catid'"; //这里也加上,单引变双引你有木有看见,99的反斜杠也不见
- if(intval($cityid)!=0)
- $sql .= ' AND `city`=\''.$cityid.'\'';
- foreach ($_GET as $k=>$r) {
- if(in_array($k,$fields_key) && intval($r)!=0 && ($fields[$k]['filtertype'] || $fields[$k]['rangetype'])) {
- if($fields[$k]['formtype'] == 'linkage') {
- $datas = getcache($fields[$k]['linkageid'],'linkage');
- $infos = $datas['data'];
- if($infos[$r]['arrchildid']) {
- $sql .= ' AND `'.$k.'` in('.$infos[$r]['arrchildid'].')';
- }
- } elseif($fields[$k]['rangetype']) {
- if(is_numeric($r)) {
- $sql .=" AND `$k` = '$r'";
- } else {
- $fieldvalue = explode('_',$r);
- $min = intval($fieldvalue[0]);
- $max = $fieldvalue[1] ? intval($fieldvalue[1]) : 999999;
- $sql .=" AND `$k` >= '$min' AND`$k` < '$max'";
- }
- }
- elseif($fields[$k]['boxtype']=='checkbox' || $fields[$k]['boxtype']=='multiple'){ //这里也改啦
- $sql .=" AND `$k` LIKE '%,$r,%'"; //为的是复选或多选的筛选
- } else {
- $sql .=" AND `$k` = '$r'";
- }
- }
- }
- return $sql;
- return $k;
- }
亲自试过,成功解决。