PHPCMS列表联动筛选功能开发的详细教程

前端之家收集整理的这篇文章主要介绍了PHPCMS列表联动筛选功能开发的详细教程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHPcms实现联动筛选的教程目前还没有,下面CMS资源网就给大家分享一下PHPcms如何实现列表页联动筛选,筛选效果截图如下:
图01
话不多说,下面开始详细教程:
首先用下面代码替换掉PHPcms/libs/functions/extention.func.PHP文件里面的代码
PHP">$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'].'' : ''.$v['name'].'' ;
			}
			}
			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('model_field_'.$modelid,'model');
        if(is_array($fields) && !empty($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 = empty($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].'' : ''.$v[0].'' ;
	}
	$all['name'] = '全部';
	$all['url'] = structure_filters_url($field,array($field=>''),$modelid);
	$all['menu'] = $field_value == '' ? ''.$all['name'].'' : ''.$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 || empty($field)) return false;
	$fields = getcache('model_field_'.$modelid,'model');
	$keyid = $fields[$field]['linkageid'];
	$datas = getcache($keyid,'linkage');
	$infos = $datas['data'];
                
	if(empty($linkageid)){
		$linkageid = intval($_GET[$field]);
		if(!$linkageid) return false;
		}
		
		$urlrule = structure_filters_url($field,array(),$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,$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;
									}

/**
 * 分页,如去掉则分页会有问题
 */
function makeurlrule() {
	if(strpos(URLRULE,'.html') === FALSE) {
		return url_par('page={$'.'page}');
	}
	else {
		$url = preg_replace('/-[0-9]+.html$/','-{$page}.html',get_url());
		return $url;
	}
}
?>
如果大家不想复制代码,也可以直接下载下面的文件覆盖即可,下载地址如下:

 
说明一下,这里用的是模型里的下拉框,模板里面的调用代码如下:
性别:
 {php $xingbie_rang = array('男|1','女|2','组合|3')}
{loop filters('xingbie',$xingbie_rang) $r}
{$r[menu]}
{/loop}

{php $sql = structure_filters_sql($modelid)}
{php $urlrule = makeurlrule()}
 {pc:content action="lists" catid="$catid" where="$sql" num="35" order="id DESC"page="$page" moreinfo="1" urlrule="$urlrule"}
{loop $data $r}

{/loop} {/pc}
上面的代码是以性别为例的,大家可以自己修改
如果要实现伪静态,可以参考下面的伪静态规则:
PHP?m=content&c=index&a=lists&catid=$1&page=$2;
rewrite ^/list-([0-9]+)-(.*)-(.*)-(.*)-(.*)-([0-9]+).html /index.PHP?m=content&c=index&a=lists&catid=$1&diqu=$2&xingbie=$3&xingzuo=$4&zimu=$5&page=$6;


原文链接:https://www.f2er.com/phpcms/403702.html

猜你在找的phpcms相关文章