多条件筛选的实现

<?php
$conditions = array('price','color','metal');  //要进行筛选的字段放在这里
$price = $color = $metal='';  //先给需要筛选的字段赋空值,这些值将输出到页面的hidden fileds中
//以下循环给已经进行的筛选赋值,以便能够在下一次筛选中保留
foreach($conditions as $value){
    if(isset($_POST[$value])){
        $$value = $_POST[$value];
    }
}
//以下是演示输出$_POST数据
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分类筛选演示</title>
<style type="text/css">
body{font-size:14px;font-family:Tahoma,"宋体"}
</style>
<script type="text/javascript">
function Filter(a,b){
    var $ = function(e){return document.getElementById(e);}
    var ipts = $('filterForm').getElementsByTagName('input'),result=[];
    for(var i=0,l=ipts.length;i<l;i++){
        if(ipts[i].getAttribute('to')=='filter'){
           result.push(ipts[i]);
       }
    }
   if($(a)){
        $(a).value = b;
        for(var j=0,len=result.length;j<len;j++){
           if(result[j].value==''){
               result[j].parentNode.removeChild(result[j]);
           }
       }
        document.forms['filterForm'].submit();
    }
    return false;
}
</script>
</head>
<body>
<form id="filterForm" action="listinfo.php" method="post">
<input to="filter" type="hidden" id="price" name="price" value="<?=$price?>" />
<input to="filter" type="hidden" id="color" name="color" value="<?=$color?>" />
<input to="filter" type="hidden" id="metal" name="metal" value="<?=$metal?>" />
</form>
价格:<a href="javascript:Filter('price','100-1000');">100-1000</a>
      <a href="javascript:Filter('price','1001-2000');">1001-2000</a>
     <a href="javascript:Filter('price','2001-3000');">2001-3000</a><br/>
颜色:<a href="javascript:Filter('color','红色');">红色</a>
      <a href="javascript:Filter('color','蓝色');">蓝色</a><br />
材质:<a href="javascript:Filter('metal','纯金');">纯金</a>
      <a href="javascript:Filter('metal','纯银');">纯银</a><br />
</body>
</html><br>// 将以上文件保存成listinfo.php运行就是筛选的效果!

 

你可能感兴趣的:(实现)