首頁  >  文章  >  php教程  >  经典分类筛选

经典分类筛选

PHP中文网
PHP中文网原創
2016-05-23 08:39:071733瀏覽

2.png

url.php

<?php
header(&#39;Content-Type:text/html;charset=utf-8&#39;);
 
/**
 * 生成分类信息中的筛选菜单
 * @param $field   字段名称
 * @param $modelid  模型ID
 * @param $diyarr 数据包
 * @param $isall 是否显示全部
 */
function info_filters($field = &#39;&#39;,$diyarr = array(),$groups=array(),$isall = 1) {
  $options = $diyarr;
  $field_value = isset($_GET[$field])?intval($_GET[$field]):&#39;&#39;;
  foreach($options as $k=>$v) {
    $option[$k][&#39;name&#39;] = $v;
    $option[$k][&#39;value&#39;] = $k;
    $option[$k][&#39;url&#39;] = info_filters_url($field,array($field=>$k),$groups);
    $option[$k][&#39;menu&#39;] = $field_value == $k ? &#39;<a href="#" class="hover">&#39;.$v.&#39;</a>&#39; : &#39;<a href="&#39;.$option[$k][&#39;url&#39;].&#39;">&#39;.$v.&#39;</a>&#39;;
  }
  if ($isall) {
    $all[&#39;name&#39;] = &#39;全部&#39;;
    $all[&#39;url&#39;] = info_filters_url($field,array($field=>&#39;&#39;),$groups);
    $all[&#39;menu&#39;] = $field_value == &#39;&#39; ? &#39;<a href="#" class="hover">&#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_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 =&#39;&#39;;
    foreach ($fields as $_v=>$_k) {
        $urlpars .= &#39;&&#39;.$_v.&#39;={$&#39;.$_v.&#39;}&#39;;
    }
  }
   $urlrule =&#39;urlurl.php?m=content&c=index&a=lists&catid=1&#39;.$urlpars;
  //根据get传值构造URL
 if (is_array($array)) foreach ($array as $_k=>$_v) {
    if($_k==&#39;page&#39;) $_v=1;
    $_findme[] = &#39;/{\$&#39;.$_k.&#39;}/&#39;;
    $_v = intval($_v);
    $_replaceme[] = $_v;
  }
  //构造排除该字段名称的正则
  $filter = &#39;&#39;;
  $_findme[] = &#39;/{\$&#39;.$filter.&#39;([a-z0-9_]+)}/&#39;; 
  $_replaceme[] = &#39;&#39;;
  $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(
                                 &#39;dest&#39;=>array(1=>&#39;北京&#39;,2=>&#39;上海&#39;,3=>&#39;天津&#39;),
                                 &#39;action&#39;=>array(1=>&#39;香港&#39;,2=>&#39;澳门&#39;,3=>&#39;台湾&#39;)
                          );
                   $dest_list = info_filters(&#39;dest&#39;,$groups[&#39;dest&#39;],$groups);
                   $action_list = info_filters(&#39;action&#39;,$groups[&#39;action&#39;],$groups);
                   echo &#39;<span>出发城市:</sapn>&#39;;
                   foreach($dest_list as $val){
                   echo $val[&#39;menu&#39;].&#39;  &#39;;
                   }
                   echo &#39;<br/><span>目标城市:</sapn>&#39;;
                   foreach($action_list as $val){
                   echo $val[&#39;menu&#39;].&#39;  &#39;;
                   }
               ?>
            </div>
        </div>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn