PHP 多条件查询之简单租房系统
注:这里只展示多条件查询页面,其余的增删该页面略过
<!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>
<?php
include("DBDA.class.php");
$db = new DBDA();
//写sql语句查询出具体的区域来,以数组的形式返回,房屋类型、租赁类型的查询与此相同
$sqy = "select distinct suozaiquyu from fangyuan";
$aqy =$db->Query($sqy);
//查询租赁类型
$szl = "select distinct zulinleixing from fangyuan";
$azl =$db->Query($szl);
//查询查询房屋类型
$sfw = "select distinct fangwuleixing from fangyuan";
$afw =$db->Query($sfw);
?>
<body>
<form action="chaxun.php" method="post">
<!--第二步加条件-->
<!--在这里不需要name,但需要一个id,后面要用js进行控制-->
<div>区域:<input type="checkbox" id="qyqx" onclick="Quanxuan(this,'qy')"/>全选</div><br />
<!--此处需要显示每个详细的区域,在上面的php代码中已经写好sql语句,此处需要遍历出来-->
<div>
<?php
foreach($aqy as $v)
{//因为后面要做全选,所以在input里面加一个class和 name,后面的租赁类型和房屋类型与此相同
echo"<input class='qy' name='qy[]' type='checkbox' value='{$v[0]}'/>{$v[0]} ";
}
?>
</div><br />
<div>租赁类型:<input type="checkbox" id="zlqx" onclick="Quanxuan(this,'zl')"/>全选</div><br />
<div>
<?php
foreach($azl as $v)
{
echo"<input class='zl' name='zl[]' type='checkbox' value='{$v[0]}'/>{$v[0]} ";
}
?>
</div><br />
<div>房屋类型:<input type="checkbox" id="fwqx" onclick="Quanxuan(this,'fw')" />全选</div><br />
<div>
<?php
foreach($afw as $v)
{
echo"<input class='fw' name='fw[]' type='checkbox' value='{$v[0]}'/>{$v[0]} ";
}
?>
</div><br />
<div>关键字:<input type="text" name="key" /></div>
<br />
<input type="submit" value="搜索" />
</form><br />
<!--第一步建表,最终的查询结果以数据表的形式呈现出来-->
<table width="70%" border="1" cellpadding="0" cellspacing="0" style="text-align:center">
<tr>
<td>所在区域</td>
<td>关键字</td>
<td>面积</td>
<td>租赁价格</td>
<td>租赁类型</td>
<td>房屋类型</td>
</tr>
<?php
//接收查询条件并处理
$tj1 = " 1=1";
$tj2 = " 1=1";
$tj3 = " 1=1";
$tj4 = " 1=1";
if(!empty($_POST))
{
if(!empty($_POST["qy"]))
{//Area in ('aa','bb','cc')
$str1 = implode("','",$_POST["qy"]);
$tj1= "suozaiquyu in ('{$str1}') ";
}
if(!empty($_POST["zl"]))
{
$str2 = implode("','",$_POST["zl"]);
$tj2= "zulinleixing in ('{$str2}') ";
}
if(!empty($_POST["fw"]))
{
$str3 = implode("','",$_POST["fw"]);
$tj3= "fangwuleixing in ('{$str3}') ";
}
if($_POST["key"]!="")
{
$tj4 = "keyword like '%{$_POST['key']}%'";
}
}
//查询总条件
$ztj = " where {$tj1} and {$tj2} and {$tj3} and {$tj4}";
$sql = "select * from fangyuan".$ztj;
$attr = $db->Query($sql);
foreach($attr 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">
function Quanxuan(a,b)//a是onclick本身,b是class名
{
//alert(a.checked);//可以查看全选状态
var ck = document.getElementsByClassName(b);//根据传过来的b,找到a对应的下一级checkbox
//把ck的选中状态变的跟a一样,利用for循环来做
for(var i=0;i<ck.length;i++)
{
if(a.checked)
{
ck[i].setAttribute("checked","checked");//选中
}
else
{
ck[i].removeAttribute("checked");//不选中
}
}
}
</script>
</html>