查询的例子,房屋租赁

难点:1.全选功能。用到js解决。

           2.点击搜索,查出结果,同时查询的条件保持选中状态。判断提交的数组是否包含输出的条件,包含的话,输出的条件样式为选中状态。

   

我做的:

<!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>
</head>

<body>
<form action="sousuo.php" method="post">
<?php
$tj = " 1=1 ";
$tjarea = " 1=1 ";
$tjrenttype = " 1=1 ";
$tjhousetype = " 1=1 ";
$keyword = "";
if(!empty($_POST["keyword"])){
    $keyword = $_POST["keyword"];
    $tj = " keyword like '%{$keyword}%' ";
    }
$area = array();    
if(!empty($_POST["area"])){
    $area = $_POST["area"];
    $areastr = implode("','",$area);
    $tjarea = " area in ('{$areastr}') " ;
    }
$renttype = array();    
if(!empty($_POST["renttype"])){
    $renttype = $_POST["renttype"];
    $renttypestr = implode("','",$renttype);
    $tjrenttype = " renttype in ('{$renttypestr}') " ;
    }
$housetype = array();
if(!empty($_POST["housetype"])){
    $housetype = $_POST["housetype"];
    $housetypestr = implode("','",$housetype);
    $tjhousetype = " housetype in ('{$housetypestr}') " ;
    }
require_once "./DBDA.class.php";
$db = new DBDA();
$sql = "select distinct area from house";//查取区域
$sql1 = "select distinct renttype from house";//查取租赁类型
$sql2 = "select distinct housetype from house";//房屋类型
$arr = $db->query($sql);
$arr1 = $db->query($sql1);
$arr2 = $db->query($sql2);
/*var_dump($arr);
var_dump($arr1);
var_dump($arr2);*/
echo "<div>区域:<input type='checkbox' id='qy' />全选</div>";
foreach($arr as $v){
    if(in_array($v[0],$area)){
        echo "<input type='checkbox' class='qy' name='area[]'value='{$v[0]}' checked='checked'>{$v[0]} ";
        }else{
        echo "<input type='checkbox' class='qy' name='area[]'value='{$v[0]}'>{$v[0]}";
        }
    }
    echo "<br>";
    echo "<div>租赁类型:<input type='checkbox' id='zl' />全选</div>";
foreach($arr1 as $v1){
    if(in_array($v1[0],$renttype)){
        echo "<input type='checkbox' class='zl' name='renttype[]'value='{$v1[0]}' checked='checked'>{$v1[0]}";
        }else{
            echo "<input type='checkbox' class='zl' name='renttype[]'value='{$v1[0]}'>{$v1[0]}";
            }
    
    }

echo "<br>";
echo "<div>房屋类型:<input type='checkbox' id='fw' />全选</div>";
foreach($arr2 as $v2){
    if(in_array($v2[0],$housetype)){
        echo "<input type='checkbox' class='fw' name='housetype[]'value='{$v2[0]}' checked='checked'>{$v2[0]}";
        }else{
            echo "<input type='checkbox' class='fw' name='housetype[]'value='{$v2[0]}'>{$v2[0]}";
            }
    
    }
echo "<div>关键字:<input type='text' name='keyword' value='{$keyword}'></div>
<input type='submit' value='搜索'>";
?>

</form>
<table width="100%" border="1" celpadding="0" cellpadding="0">
<tr>
    <td>id</td>
    <td>关键字</td>
    <td>区域</td>
    <td>使用面积</td>
    <td>租金</td>
    <td>租赁类型</td>
    <td>房屋类型</td>
</tr>
<?php

    
require_once "./DBDA.class.php";
$db = new DBDA();
/*$sql3 = "select * from house  where area in ('{$areastr}')";
$sql4 = "select * from house  where renttype in ('{$renttypestr}')";
$sql5 = "select * from house  where housetype in ('{$housetypestr}')";
$sql6 = "select * from house where {$tj} ";

$sql8 = "select * from house  where area in ('{$areastr}');";
$sql7 = $sql3.$sql4.$sql5.$sql6;

var_dump($result);*/
 $sql4 = "select * from house where {$tj}and {$tjarea}and {$tjrenttype}and {$tjhousetype}";
$arr = $db->query($sql4);
/*var_dump($result);*/
    foreach($arr as $v){
        echo "<tr>
    <td>{$v[0]}</td>
    <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>
</body>
<script type="text/javascript">
var qy = document.getElementById("qy");
qy.onclick = function(){
    var zt = qy.checked;
    var ck = document.getElementsByClassName("qy");
    for(var i=0;i<ck.length;i++){
        ck[i].checked = zt;
        }
    }

var zl = document.getElementById("zl");
zl.onclick = function(){
    var zt = zl.checked;
    var ck = document.getElementsByClassName("zl");
    for(var i=0;i<ck.length;i++){
        ck[i].checked = zt;
        }
    }
    
var fw = document.getElementById("fw");
fw.onclick = function(){
    var zt = fw.checked;
    var ck = document.getElementsByClassName("fw");
    for(var i=0;i<ck.length;i++){
        ck[i].checked = zt;
        }
    }
</script>
</html>

上课讲的:

<!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>
</head>

<body>
<h1>租房子</h1>
<?php
require_once "../crud/DBDA.class.php";
$db = new DBDA();

$tj1 = " 1=1 ";
$tj2 = " 1=1 ";
$tj3 = " 1=1 ";
$tj4 = " 1=1 ";
$arr1 = array();
if(!empty($_POST["qy"])){
    // area in ('','','','')
    $arr1 = $_POST["qy"];
    $str = implode("','",$arr1);
    $tj1 = " area in ('{$str}') ";
}
$arr2 = array();
if(!empty($_POST["zl"])){
    // area in ('','','','')
    $arr2 = $_POST["zl"];
    $str = implode("','",$arr2);
    $tj2 = " renttype in ('{$str}') ";
}
$arr3 = array();
if(!empty($_POST["fw"])){
    // area in ('','','','')
    $arr3 = $_POST["fw"];
    $str = implode("','",$arr3);
    $tj3 = " housetype in ('{$str}') ";
}
$key = "";
if(!empty($_POST["key"])){
    $key = $_POST["key"];
    $tj4 = " keyword like '%{$key}%' ";
}
?>
<form action="main.php" method="post">
<div>
区域:<input type="checkbox" id="qy" />全选<br />
<?php
$sqlqy = "select distinct area from housedb";
$arrqy = $db->query($sqlqy);
foreach($arrqy as $v){
    if(in_array($v[0],$arr1)){
        echo "<input class='qy' type='checkbox' name='qy[]' value='{$v[0]}' checked='checked' />{$v[0]} &nbsp;";
    }else{
        echo "<input class='qy' type='checkbox' name='qy[]' value='{$v[0]}' />{$v[0]} &nbsp;";
    }
    
}
?>
</div>
<br />

<div>
租赁类型:<input type="checkbox" id="zl" />全选<br />
<?php
$sqlzl = "select distinct renttype from housedb";
$arrzl = $db->query($sqlzl);
foreach($arrzl as $v){
    if(in_array($v[0],$arr2)){
        echo "<input class='zl' type='checkbox' name='zl[]' value='{$v[0]}' checked='checked' />{$v[0]} &nbsp;";
    }else{
        echo "<input class='zl' type='checkbox' name='zl[]' value='{$v[0]}' />{$v[0]} &nbsp;";
    }
    
}
?>
</div>
<br />

<div>
房屋类型:<input type="checkbox" id="fw" />全选<br />
<?php
$sqlfw = "select distinct housetype from housedb";
$arrfw = $db->query($sqlfw);
foreach($arrfw as $v){
    if(in_array($v[0],$arr3)){
        echo "<input class='fw' type='checkbox' name='fw[]' value='{$v[0]}' checked='checked' />{$v[0]} &nbsp;";
    }else{
        echo "<input class='fw' type='checkbox' name='fw[]' value='{$v[0]}' />{$v[0]} &nbsp;";
    }
}
?>
</div>
<br />
<div>
关键字:<input type="text" name="key" value='<?php echo $key; ?>' />
</div>
<br />
<div>
<input type="submit" value="搜索" />
</div>
<br />

</form>

<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>关键字</td>
        <td>区域</td>
        <td>建筑面积</td>
        <td>租金</td>
        <td>租赁类型</td>
        <td>房屋类型</td>
    </tr>
    <?php

    
    $sql = "select * from housedb where {$tj1} and {$tj2} and {$tj3} and {$tj4}";
    echo $sql;
    $arr = $db->query($sql);
    foreach($arr 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>
</body>
<script type="text/javascript">
var qy = document.getElementById("qy");
qy.onclick = function(){
    var zt = qy.checked;
    var ck = document.getElementsByClassName("qy");
    for(var i=0;i<ck.length;i++){
        ck[i].checked = zt;
    }
}
var zl = document.getElementById("zl");
zl.onclick = function(){
    var zt = zl.checked;
    var ck = document.getElementsByClassName("zl");
    for(var i=0;i<ck.length;i++){
        ck[i].checked = zt;
    }
}
var fw = document.getElementById("fw");
fw.onclick = function(){
    var zt = fw.checked;
    var ck = document.getElementsByClassName("fw");
    for(var i=0;i<ck.length;i++){
        ck[i].checked = zt;
    }
}
</script>
</html>
原文地址:https://www.cnblogs.com/niushuangmeng/p/8323671.html