<body> <h2>租赁信息</h2><br /> <div> <form action="zufang.php" method="post"> <div>区 域: <input type="checkbox" onclick="CheckAll(this,'qy')" />全选</div> <?php require_once "DBDB.class.php"; $db = new DBDB(); $sqlqy = "select distinct Area from house"; $attrqy = $db->Query($sqlqy); //var_dump($attrqy); foreach($attrqy as $v) { echo "<input class='qy' type='checkbox' value='{$v[0]}' name='qy[]'/>{$v[0]} "; } ?> <div><br />租赁类型:<input type="checkbox" onclick="CheckAll(this,'zl')"/>全选</div> <div> <?php $sqlzl = "select distinct RentType from House"; $attrzl = $db->Query($sqlzl); foreach($attrzl as $v) { echo "<input class='zl' type='checkbox' value='{$v[0]}' name='zl[]'/>{$v[0]} "; } ?> </div><br /> <div>房屋类型:<input type="checkbox" onclick="CheckAll(this,'fw')" />全选</div> <div> <?php $sqlfw = "select distinct HouseType from House"; $attrfw = $db->Query($sqlfw); //var_dump($attrqy); foreach($attrfw as $v) { echo "<input class='fw' type='checkbox' value='{$v[0]}' name='fw[]'/>{$v[0]} "; } ?> </div><br /> <div >关 键 字: <input type="text" name="keyword" id="key" /> <br /> <input type="submit" value="搜索" /> </form> <br /> <br /> <br /> <table border="2" width="80%" style="text-align:center;" > <tr> <td>关键字</td> <td>区域</td> <td>面积</td> <td>租金</td> <td>租赁类型</td> <td>房屋类型</td> </tr> <?php $tj = ""; $tj1 = " 1=1 ";//条件恒成立注意空格 $tj2 = " 1=1 "; $tj3 = " 1=1 "; $tj4 = " 1=1 "; if(!empty($_POST["qy"])) {
// area in ('','','','') $attr = $_POST["qy"]; $str = implode("','",$attr);//拼接字符串 $tj1 = " Area in ('{$str}')"; } if(!empty($_POST["zl"])) { $attr = $_POST["zl"]; $str = implode("','",$attr); $tj2 = " RentType in ('{$str}')"; } if(!empty($_POST["fw"])) { $attr = $_POST["fw"]; $str = implode("','",$attr); $tj3 = " HouseType in ('{$str}')"; } if(!empty($_POST["keyword"])) { $attr = $_POST["keyword"]; $tj3 = " keyword like '%{$attr}%'"; } $tj = " Where {$tj1} and {$tj2} and {$tj3} and {$tj4}";//Where 前加空格 $sql = "select * from House".$tj;//拼接 $attrall = $db->Query($sql); //var_dump($attrall); foreach($attrall as $v) { echo "<tr> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> <td>{$v[6]}</td> </tr>"; } ?> </table> </div> <script type="text/javascript"> function CheckAll(a,b)//this表示该 { var qx = a.checked; var ck = document.getElementsByClassName(b); for(var i =0;i<ck.length;i++) { ck[i].checked = qx; } } </script> </body>
保持选中查询后条件还是在选中状态
<body> <h2>租赁信息</h2><br /> <div> <form action="zufang.php" method="post"> <div>区 域: <input type="checkbox" onclick="CheckAll(this,'qy')" />全选</div> <?php require_once "DBDB.class.php"; $db = new DBDB(); $tj = ""; $tj1 = "1=1"; $tj2 = "1=1"; $tj3 = "1=1"; $tj4 = "1=1"; $attr1 = array(); if(!empty($_POST["qy"])) { $attr1 = $_POST["qy"]; $str = implode("','",$attr1); $tj1 = " Area in ('{$str}')"; } $attr2 = array(); if(!empty($_POST["zl"])) { $attr2 = $_POST["zl"]; $str = implode("','",$attr2); $tj2 = " RentType in ('{$str}')"; } $attr3 = array(); if(!empty($_POST["fw"])) { $attr3 = $_POST["fw"]; $str = implode("','",$attr3); $tj3 = " HouseType in ('{$str}')"; } $attr4 = ""; if(!empty($_POST["keyword"])) { $attr4 = $_POST["keyword"]; $tj3 = " keyword like '%{$attr4}%'"; } //把if语句拿到上面 拿到复选框的上面,把每个attr拿到花括号外面 $sqlqy = "select distinct Area from house"; $attrqy = $db->Query($sqlqy); //var_dump($attrqy); foreach($attrqy as $v)//加if语句 { if(in_array($v[0],$attr1)){ //判断$v[0] 是否在数组attr1中 遍历出的$v[0]是否被选中 echo "<input class='qy' type='checkbox' checked='checked' value='{$v[0]}' name='qy[]'/>{$v[0]} "; }else{ echo "<input class='qy' type='checkbox' value='{$v[0]}' name='qy[]'/>{$v[0]} "; } } ?> <div><br />租赁类型:<input type="checkbox" onclick="CheckAll(this,'zl')"/>全选</div> <div> <?php $sqlzl = "select distinct RentType from House"; $attrzl = $db->Query($sqlzl); foreach($attrzl as $v) { if(in_array($v[0],$attr2)){ echo "<input class='zl' type='checkbox' checked='checked' value='{$v[0]}' name='zl[]'/>{$v[0]} "; }else{ echo "<input class='zl' type='checkbox' value='{$v[0]}' name='zl[]'/>{$v[0]} "; } } ?> </div><br /> <div>房屋类型:<input type="checkbox" onclick="CheckAll(this,'fw')" />全选</div> <div> <?php $sqlfw = "select distinct HouseType from House"; $attrfw = $db->Query($sqlfw); //var_dump($attrqy); foreach($attrfw as $v) { if(in_array($v[0],$attr3)){ echo "<input class='fw' type='checkbox' checked='checked' value='{$v[0]}' name='fw[]'/>{$v[0]}"; }else{ echo "<input class='fw' type='checkbox' value='{$v[0]}' name='fw[]'/>{$v[0]}"; } } ?> </div><br /> <div >关 键 字: <input type="text" name="keyword" id="key" value='<?php echo $attr4; ?>'/><!--加入一小段php代码--> <br /> <input type="submit" value="搜索"/> </form> <br /> <br /> <br /> <table border="2" width="80%" style="text-align:center;" > <tr> <td>关键字</td> <td>区域</td> <td>面积</td> <td>租金</td> <td>租赁类型</td> <td>房屋类型</td> </tr> <?php $tj = " Where {$tj1} and {$tj2} and {$tj3} and {$tj4}";//Where 前加空格 $sql = "select * from House".$tj; $attrall = $db->Query($sql); //var_dump($attrall); foreach($attrall as $v) { echo "<tr> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> <td>{$v[6]}</td> </tr>"; } ?> </table> </div> <script type="text/javascript"> function CheckAll(a,b)//this表示该 { var qx = a.checked; var ck = document.getElementsByClassName(b); for(var i =0;i<ck.length;i++) { ck[i].checked = qx; } } </script> </body>
查询完成条件还是选中状态 和第一个不一样 第一个查询后条件就变为空