首先感谢想到即可做到大神的无私分享,原来的代码看这里: http://bbs.phome.net/showthread-13-355102-9.html
我做了一点改进,增加了 不限 选择,使得操作更加人性化。
代码如下: function retUrl($field, $fval, $mult = 1) {
$urlPares = $GLOBALS['urlPares'];
// 如果结合项字段在GET中,写入参数
foreach ($GLOBALS['fieldData'] as $key => $value) if (isset($_GET[$value])) $urlPares[$value] = $_GET[$value];
$res = '';
// 结合项单选
if (!$mult || !isset($_GET[$field])) {
$copy = $urlPares;
foreach ($fval as $key => $value) {
$css = '';
// 如果url中有此参数 则删除
if (isset($_GET[$field]) && $_GET[$field] == $value) {
unset($copy[$field]);
$css = 'active';
} else {
if($value=='不限'){
if(!isset($copy[$field])){
$css = 'active';
}
unset($copy[$field]);
}else{
$copy[$field] = $value;
}
}
$tpl = '/e/action/ListInfo.php?'.http_build_query($copy);
$tpl = urldecode($tpl);
$res .= sprintf('<a class="%s" href="%s">%s</a>', $css, $tpl, $value);
}
return $res;
}
// 结合项多选
// 如果url中不是数组形式
if (!is_array($_GET[$field])) {
foreach ($fval as $key => $value) {
$css = '';
// 如果当前值在url中
$copy = $urlPares;
if ($value == $_GET[$field]) {
$css = 'active';
unset($copy[$field]);
} else {
if($value=='不限'){
unset($copy[$field]);
}else{
$copy[$field] = array();
$copy[$field][] = $_GET[$field];
$copy[$field][] = $value;
}
}
$tpl = '/e/action/ListInfo.php?'.http_build_query($copy);
$tpl = urldecode($tpl);
$res .= sprintf('<a class="%s" href="%s">%s</a>', $css, $tpl, $value);
}
return $res;
}
// 数组形式的
foreach ($fval as $key => $value) {
$css = '';
$copy = $urlPares;
// 如果url中存在则删除
if (in_array($value, $copy[$field])) {
unset($copy[$field][array_search($value, $copy[$field])]);
$css = 'active';
} else {
if($value=='不限'){
unset($copy[$field]);
}else{
$copy[$field][] = $value;
}
}
if(isset($copy[$field]))sort($copy[$field]);
$tpl = '/e/action/ListInfo.php?'.http_build_query($copy);
$tpl = urldecode($tpl);
$res .= sprintf('<a class="%s" href="%s">%s</a>', $css, $tpl, $value);
}
return $res;
}
|