php 数据访问练习:租房查询页面

<html>
<head>
<title></title>
<meta charset="UTF-8"/>

<link rel="stylesheet" type="text/css" href="bootstrap.min.css"/>
<script src="bootstrap.min.js"></script>
<script src="jquery-1.11.2.min.js"></script>
</head>

<form action="zufangzi.php" method="get">
<body>
<?php

$db = new MySQLi("localhost","root","","php_zuoye");
if(mysqli_connect_error()){
echo "连接失败!";
exit;
}

//输入关键字搜索查询条件及语句
$name = "";
$sq = " 1=1 ";
if(!empty($_GET["chaxun"])){
$name = $_GET["chaxun"];
$sq = " KeyWord like '%{$name}%' or
housetype like '%{$name}%' ";
}

// 区域查询条件及语句
$sq2 = " 1=1 ";
@$name2 = $_GET['xuanxiang'];
// var_dump($name2);
if(!empty($name2)){
@$sq2 = " area in ('{$name2[0]}','{$name2[1]}',
'{$name2[2]}','{$name2[3]}','{$name2[4]}') ";
}

//租赁类型查询条件及语句
$sq3 = " 1=1 ";
@$name3 = $_GET['zulx'];
// var_dump($name3);
if(!empty($name3)){
@$sq3 = " renttype in ('{$name3[0]}','{$name3[1]}',
'{$name3[2]}') ";
}

//房屋类型查询及语句
$sq4 = " 1=1 ";
@$name4 = $_GET['fanglx'];
// var_dump($name4);
if(!empty($name4)){
@$sq4 = " housetype in ('{$name4[0]}','{$name4[1]}',
'{$name4[2]}','{$name4[3]}','{$name4[4]}','{$name4[5]}') ";
}
?>

//页面框架
<div class="panel panel-primary">
<div class="panel-heading"><!--标题-->
<h3 class="panel-title">老刘租房</h3>
</div>
<div class="panel-body">
<div id="" style="float: left;
margin-left: 40px;"><!--顶部第1部分开始-->
<div><!--区域-->
<span id="">
区&nbsp;&nbsp;&nbsp;域:
</span>
<label class="checkbox-inline">
<input type="checkbox" id="quyu"
onclick="quanxuan(quyu,quyu1)">
全选
</label>
</div>
<div>
<label class="checkbox-inline">
<input type="checkbox" class="quyu1"
name="xuanxiang[0]" value="潘庄">
潘庄
</label>
<label class="checkbox-inline">
<input type="checkbox" class="quyu1"
name="xuanxiang[1]" value="理工大学">
理工大学
</label>
<label class="checkbox-inline">
<input type="checkbox" class="quyu1"
name="xuanxiang[2]" value="区政府">
区政府
</label>
<label class="checkbox-inline">
<input type="checkbox" class="quyu1"
name="xuanxiang[3]" value="钻石商务大厦">
钻石商务大厦
</label><label class="checkbox-inline">
<input type="checkbox" class="quyu1"
name="xuanxiang[4]" value="火车站">
火车站
</label>
</div><br />
</div><!--顶部第1部分结束-->

<div id="" style="float: left;
margin-left: 40px;"><!--顶部第2部分开始-->
<div id="">
<span id="">
租赁类型:
</span>
<label class="checkbox-inline">
<input type="checkbox" id="zulx"
onclick="quanxuan(zulx,zulx1)">
全选
</label>
</div>
<div>
<label class="checkbox-inline">
<input type="checkbox" class="zulx1"
name="zulx[0]" value="床位">
床位
</label>
<label class="checkbox-inline">
<input type="checkbox" class="zulx1"
name="zulx[1]" value="合租">
合租
</label>
<label class="checkbox-inline">
<input type="checkbox" class="zulx1"
name="zulx[2]" value="整租">
整租
</label>
</div><br />
</div><!--顶部第2部分结束-->

<div id="" style="float: left;
margin-left: 40px;"><!--顶部第3部分开始-->
<div id="">
<span id="">
房屋类型:
</span>
<label class="checkbox-inline">
<input type="checkbox" id="fanglx"
onclick="quanxuan(fanglx,fanglx1)">
全选
</label>
</div>
<div>
<label class="checkbox-inline">
<input type="checkbox" class="fanglx1"
name="fanglx[0]" value="1室1厅">
1室1厅
</label>
<label class="checkbox-inline">
<input type="checkbox" class="fanglx1"
name="fanglx[1]" value="2室1厅">
2室1厅
</label>
<label class="checkbox-inline">
<input type="checkbox" class="fanglx1"
name="fanglx[2]" value="3室2厅">
3室2厅
</label>
<label class="checkbox-inline">
<input type="checkbox" class="fanglx1"
name="fanglx[3]" value="4室2厅">
4室2厅
</label>
<label class="checkbox-inline">
<input type="checkbox" class="fanglx1"
name="fanglx[4]" value="平房">
平房
</label>
<label class="checkbox-inline">
<input type="checkbox" class="fanglx1"
name="fanglx[5]" value="筒子楼">
筒子楼
</label>
</div><br />
</div><!--顶部第3部分结束-->

<div id="" style="position: absolute; right: 30px;"><!--顶部第4部分开始-->
<input type="text" class="form-control" id="name"
placeholder="输入关键字查询" style="max-width: 160px;"
name="chaxun" value="<?php echo $name ?>"
onclick="">
<button type="submit" class="btn btn-primary
btn-sm">查 询</button>
</div><!--顶部第4部分结束-->
</div><!--面板内容结尾-->
</div><!--面板结尾-->


<table class="table table-striped"><!--输出数据库数据为表格-->
<thead>
<tr>
<th>关键字</th>
<th>区域</th>
<th>使用面积</th>
<th>租金</th>
<th>租住类型</th>
<th>房屋类型</th>
</tr>
</thead>
<tbody>
<?php
require_once "./page.class.php";//分页显示的php代码
$sqt = "select count(*) from house where {$sq} and {$sq2}
and {$sq3} and {$sq4}";
$sqa = $db->query($sqt);
$sqa = $sqa->fetch_all();
$page = new Page($sqa[0][0],10);

$sql = "select * from House where {$sq} and {$sq2}  //查询sql语句的汇总
and {$sq3} and {$sq4}".$page->limit;
$result = $db->query($sql);
$arr = $result->fetch_all();

foreach($arr as $v){                                                //改变关键字样式
$x = str_replace($name,"<span style='color:red;
'><b>{$name}</b></span>",$v[1]);
$y = str_replace($name,"<span style='color:red;
'><b>{$name}</b></span>",$v[6]);

echo "<tr>                                                             //输出数据库数据为表格
<td>{$x}</td>
<td>{$v[2]}</td>
<td>{$v[3]}</td>
<td>{$v[4]}</td>
<td>{$v[5]}</td>
<td>{$y}</td>
</tr>";
}
?>

</tbody>
</table>
<div style="margin-left: 350px; font-weight: bold;">  //分页显示底部栏
<?php
echo $page->fpage();
?>
</div>


</body>
</form>
</html>
<script type="text/javascript">
var quyu = document.getElementById("quyu");
var quyu1 = document.getElementsByClassName("quyu1");
var zulx = document.getElementById("zulx");
var zulx1 = document.getElementsByClassName("zulx1");
var fanglx = document.getElementById("fanglx");
var fanglx1 = document.getElementsByClassName("fanglx1");

function quanxuan(a,b){                                                     //控制全选
for(var i=0;i<=quyu1.length;i++){
b[i].checked = a.checked;
}
}
</script>

posted @ 2017-09-05 20:25  黑眼圈的鱼  阅读(226)  评论(0编辑  收藏  举报