php 数据库练习之租房子
题目:
示例图
本次只做图4这个表,因为之前的都已做过
自己在mydb数据库建了一个house表
如图:
自己做的代码:
<!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="house_main.php" method="post">
<div>
区域:
<input type="checkbox" name="qx1" onclick="checkall(this)" />全选
</div>
<div>
<?php
$db = new MySQLi("localhost","root","root","mydb");
$sqlqx = "select distinct area from house ";
$resultqx = $db->query($sqlqx);
while($arrqx = $resultqx->fetch_row())
{
echo"<input class='qx' type='checkbox' value='{$arrqx[0]}' name='qx[]' />{$arrqx[0]}";
}
?>
</div>
<div>
租赁类型:
<input type="checkbox" name="qx2" onclick="checkall2(this)"/>全选
</div>
<div>
<?php
$sqlqy = "select distinct renttype from house ";
$resultqy = $db->query($sqlqy);
while($arrqy = $resultqy->fetch_row())
{
echo"<input class='qy' type='checkbox' value='{$arrqy[0]}' name='qy[]'/>{$arrqy[0]}";
}
?>
</div>
<div>
房屋类型:
<input type="checkbox" name="qx3" onclick="checkall3(this)"/>全选
</div>
<div>
<?php
$sqlqz = "select distinct housetype from house";
$resultqz = $db->query($sqlqz);
while($arrqz = $resultqz->fetch_row())
{
echo"<input class='qz' type='checkbox' value='{$arrqz[0]}' name='qz[]' />{$arrqz[0]}";
}
?>
</div>
<div>
关键字:
<input type="text" name="keyword"/>
</form>
<br />
<input type="submit" value="搜索" />
</div>
</div>
<br />
<br />
<br />
</form>
<table width="50%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>关键字</td>
<td>区域</td>
<td>建筑面积</td>
<td>租金</td>
<td>租赁类型</td>
<td>房屋类型</td>
</tr>
<?php
$tj = "";
$tj1 = "1=1";
$tj2 = "2=2";
$tj3 = "3=3";
$tj4 = "4=4";
if(!empty($_POST["qx"]) && count($_POST["qx"]>0))
{
$attr = $_POST["qx"];
$str = implode("','",$attr);
$tj1 = "area in ('{$str}')";
}
if(!empty($_POST["qy"]) && count($_POST["qy"]>0))
{
$attr = $_POST["qy"];
$str = implode("','",$attr);
$tj2 = "renttype in ('{$str}')";
}
if(!empty($_POST["qz"]) && count($_POST["qz"]>0))
{
$attr = $_POST["qz"];
$str = implode("','",$attr);
$tj3 = "housetype in ('{$str}')";
}
if(!empty($_POST["keyword"]) && count($_POST["keyword"]>0))
{
$attr = $_POST["keyword"];
$tj4 = "keyword like '%{$attr}%'";
}
//$tj = " where {$tj1} and {$tj2} and {$tj3} and {$tj4} ";
$sql = "select * from house where {$tj1} and {$tj2} and {$tj3} and {$tj4} ";
$attry = $db->query($sql);
while($arr = $attry->fetch_row())
{
echo"<tr>
<td>{$arr[1]}</td>
<td>{$arr[2]}</td>
<td>{$arr[3]}</td>
<td>{$arr[4]}</td>
<td>{$arr[5]}</td>
<td>{$arr[6]}</td>
</tr>";
}
?>
</table>
</body>
</html>
<script type="text/javascript">
function checkall(qx)
{ //ck变量不能重复设置
var ck = document.getElementsByClassName("qx");
if(qx.checked)
{
for(var i=0;i<ck.length;i++)
{
ck[i].setAttribute("checked","checked");
}
}
else
{
for(var i=0;i<ck.length;i++)
{
ck[i].removeAttribute("checked");
}
}
}
function checkall2(qy)
{
var ck2 = document.getElementsByClassName("qy");
if(qy.checked)
{
for(var i=0;i<ck2.length;i++)
{
ck2[i].setAttribute("checked","checked");
}
}
else
{
for(var i=0;i<ck2.length;i++)
{
ck2[i].removeAttribute("checked");
}
}
}
function checkall3(qz)
{
var ck3 = document.getElementsByClassName("qz");
if(qz.checked)
{
for(var i=0;i<ck3.length;i++)
{
ck3[i].setAttribute("checked","checked");
}
}
else
{
for(var i=0;i<ck3.length;i++)
{
ck3[i].removeAttribute("checked");
}
}
}
</script>
<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="house_main.php" method="post">
<div>
区域:
<input type="checkbox" name="qx1" onclick="checkall(this)" />全选
</div>
<div>
<?php
$db = new MySQLi("localhost","root","root","mydb");
$sqlqx = "select distinct area from house ";
$resultqx = $db->query($sqlqx);
while($arrqx = $resultqx->fetch_row())
{
echo"<input class='qx' type='checkbox' value='{$arrqx[0]}' name='qx[]' />{$arrqx[0]}";
}
?>
</div>
<div>
租赁类型:
<input type="checkbox" name="qx2" onclick="checkall2(this)"/>全选
</div>
<div>
<?php
$sqlqy = "select distinct renttype from house ";
$resultqy = $db->query($sqlqy);
while($arrqy = $resultqy->fetch_row())
{
echo"<input class='qy' type='checkbox' value='{$arrqy[0]}' name='qy[]'/>{$arrqy[0]}";
}
?>
</div>
<div>
房屋类型:
<input type="checkbox" name="qx3" onclick="checkall3(this)"/>全选
</div>
<div>
<?php
$sqlqz = "select distinct housetype from house";
$resultqz = $db->query($sqlqz);
while($arrqz = $resultqz->fetch_row())
{
echo"<input class='qz' type='checkbox' value='{$arrqz[0]}' name='qz[]' />{$arrqz[0]}";
}
?>
</div>
<div>
关键字:
<input type="text" name="keyword"/>
</form>
<br />
<input type="submit" value="搜索" />
</div>
</div>
<br />
<br />
<br />
</form>
<table width="50%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>关键字</td>
<td>区域</td>
<td>建筑面积</td>
<td>租金</td>
<td>租赁类型</td>
<td>房屋类型</td>
</tr>
<?php
$tj = "";
$tj1 = "1=1";
$tj2 = "2=2";
$tj3 = "3=3";
$tj4 = "4=4";
if(!empty($_POST["qx"]) && count($_POST["qx"]>0))
{
$attr = $_POST["qx"];
$str = implode("','",$attr);
$tj1 = "area in ('{$str}')";
}
if(!empty($_POST["qy"]) && count($_POST["qy"]>0))
{
$attr = $_POST["qy"];
$str = implode("','",$attr);
$tj2 = "renttype in ('{$str}')";
}
if(!empty($_POST["qz"]) && count($_POST["qz"]>0))
{
$attr = $_POST["qz"];
$str = implode("','",$attr);
$tj3 = "housetype in ('{$str}')";
}
if(!empty($_POST["keyword"]) && count($_POST["keyword"]>0))
{
$attr = $_POST["keyword"];
$tj4 = "keyword like '%{$attr}%'";
}
//$tj = " where {$tj1} and {$tj2} and {$tj3} and {$tj4} ";
$sql = "select * from house where {$tj1} and {$tj2} and {$tj3} and {$tj4} ";
$attry = $db->query($sql);
while($arr = $attry->fetch_row())
{
echo"<tr>
<td>{$arr[1]}</td>
<td>{$arr[2]}</td>
<td>{$arr[3]}</td>
<td>{$arr[4]}</td>
<td>{$arr[5]}</td>
<td>{$arr[6]}</td>
</tr>";
}
?>
</table>
</body>
</html>
<script type="text/javascript">
function checkall(qx)
{ //ck变量不能重复设置
var ck = document.getElementsByClassName("qx");
if(qx.checked)
{
for(var i=0;i<ck.length;i++)
{
ck[i].setAttribute("checked","checked");
}
}
else
{
for(var i=0;i<ck.length;i++)
{
ck[i].removeAttribute("checked");
}
}
}
function checkall2(qy)
{
var ck2 = document.getElementsByClassName("qy");
if(qy.checked)
{
for(var i=0;i<ck2.length;i++)
{
ck2[i].setAttribute("checked","checked");
}
}
else
{
for(var i=0;i<ck2.length;i++)
{
ck2[i].removeAttribute("checked");
}
}
}
function checkall3(qz)
{
var ck3 = document.getElementsByClassName("qz");
if(qz.checked)
{
for(var i=0;i<ck3.length;i++)
{
ck3[i].setAttribute("checked","checked");
}
}
else
{
for(var i=0;i<ck3.length;i++)
{
ck3[i].removeAttribute("checked");
}
}
}
</script>
展示效果:
查询范例:
搜索结果如下: