经典分类筛选
程序员文章站
2022-03-21 12:05:13
...
url.php
<?php header('Content-Type:text/html;charset=utf-8'); /** * 生成分类信息中的筛选菜单 * @param $field 字段名称 * @param $modelid 模型ID * @param $diyarr 数据包 * @param $isall 是否显示全部 */ function info_filters($field = '',$diyarr = array(),$groups=array(),$isall = 1) { $options = $diyarr; $field_value = isset($_GET[$field])?intval($_GET[$field]):''; foreach($options as $k=>$v) { $option[$k]['name'] = $v; $option[$k]['value'] = $k; $option[$k]['url'] = info_filters_url($field,array($field=>$k),$groups); $option[$k]['menu'] = $field_value == $k ? '<a href="#" class="hover">'.$v.'</a>' : '<a href="'.$option[$k]['url'].'">'.$v.'</a>'; } if ($isall) { $all['name'] = '全部'; $all['url'] = info_filters_url($field,array($field=>''),$groups); $all['menu'] = $field_value == '' ? '<a href="#" class="hover">'.$all['name'].'</a>' : '<a href="'.$all['url'].'">'.$all['name'].'</a>'; array_unshift($option,$all); } return $option; } /** * 构造筛选URL */ function info_filters_url($fieldname, $array=array(),$fields=array(), $isphp = 0) { if(empty($array)) { $array = $_GET; } else { $array = array_merge($_GET,$array); } //TODO if(is_array($fields) && !empty($fields)) { ksort($fields); $urlpars =''; foreach ($fields as $_v=>$_k) { $urlpars .= '&'.$_v.'={$'.$_v.'}'; } } $urlrule ='urlurl.php?m=content&c=index&a=lists&catid=1'.$urlpars; //根据get传值构造URL if (is_array($array)) foreach ($array as $_k=>$_v) { if($_k=='page') $_v=1; $_findme[] = '/{\$'.$_k.'}/'; $_v = intval($_v); $_replaceme[] = $_v; } //构造排除该字段名称的正则 $filter = ''; $_findme[] = '/{\$'.$filter.'([a-z0-9_]+)}/'; $_replaceme[] = ''; $urlrule = preg_replace($_findme, $_replaceme, $urlrule); return $urlrule; } ?> <style> a{ color:#333333;} .hover{ color:#FF0000;} </style> <div class="yk_01 fix"> <div class="yy1"> <?php $groups = array( 'dest'=>array(1=>'北京',2=>'上海',3=>'天津'), 'action'=>array(1=>'香港',2=>'澳门',3=>'*') ); $dest_list = info_filters('dest',$groups['dest'],$groups); $action_list = info_filters('action',$groups['action'],$groups); echo '<span>出发城市:</sapn>'; foreach($dest_list as $val){ echo $val['menu'].' '; } echo '<br/><span>目标城市:</sapn>'; foreach($action_list as $val){ echo $val['menu'].' '; } ?> </div> </div>