PHP连接数据库实现多条件查询与分页功能——关于租房页面的完整实例操作
租房页面如图:

代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>租房子</title>
<script src="bootstrap/js/jquery-1.11.2.min.js"></script> //引入bootstrap前端框架的三个文件
<script src="bootstrap/js/bootstrap.min.js"></script>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
</head>
<style>
.yangshi{
margin-left: 69px;
}
.ys{
margin-left: 69px;
}
.juli{
margin-left: 28px;
}
</style>
<body>
<form action="rental.php" method="get" style="margin-top: -145px;"> //form表单中使用get方式进行提交
<div style="width: 80%; height: 650px; background-image: url(./img/魅力罗兰Music炫图13.jpg); margin-left: 150px; margin-top: 150px;">
<div style="margin-left: 20px;">
<h1 style="margin-left: 40%; pading-top: 20px;">租房子</h1>
<div class="juli">区域:<input type="checkbox" class="ck1" onclick="qx(this)"> 全选</div>
<div>
//连接数据库并利用去重查询取出列名为区域的这一组数据
<?php
require_once "./DBDA.class.php";
require_once "./page.class.php";
$db = new DBDA();
$sqy = "select distinct area from housedb"; //去重查询
$aqy =$db->query($sqy,0);
foreach($aqy as $v){
echo "<span class='yangshi'><input type='checkbox' class='ck1' name='qy[]' value='{$v[0]}'> {$v[0]}</span>";
}
?>
</div>
</div>
效果如图:
<div style="margin-left: 20px; margin-top: 20px;">
<div>租房类型:<input type="checkbox" class="ck2" onclick="zflx(this)"> 全选</div>
<div>
//连接数据库并利用去重查询取出列名为租房类型的这一组数据
<?php
$srt = "select distinct renttype from housedb";
$art = $db->query($srt,0);
foreach($art as $v){
echo "<span class='ys'><input type='checkbox' class='ck2' name='zflx[]' value='{$v[0]}'> {$v[0]}</span>";
}
?>
</div>
效果如图:
<div style="margin-top: 20px;">
<div class="juli">户型:<input type="checkbox" class="ck3" onclick="hx(this)"> 全选</div>
<div>
//连接数据库并利用去重查询取出列名为户型的这一组数据
<?php
$sht = "select distinct housetype from housedb";
$aht = $db->query($sht,0);
foreach($aht as $v){
echo "<span class='yangshi'><input type='checkbox' class='ck3' name='hx[]' value='{$v[0]}' > {$v[0]}</span>";
}
?>
</div>
</div>
效果如图:
</div>
<div style="margin-top: 20px; margin-left: 20px;">
<span class="glyphicon glyphicon-search" style="margin-top: 10px; float: left;"></span>
//关键字查询
<input type="text" class="form-control" name="keyword" placeholder="关键字搜索" style="max-width: 120px;float: left;">
<button type="submit" class="btn btn-danger"style="float: left; margin-left: 20px;">搜索</button>
效果如图:
</div>
//使用表格在页面输出全部数据信息
<table class="table table-bordered">
<thead>
<tr>
<th>关键字</th>
<th>区域</th>
<th>房子面积</th>
<th>租价</th>
<th>租房类型</th>
<th>户型</th>
</tr>
</thead>
<tbody>
<?php
$tj1 = " 1=1 "; //分别对不同查询的条件做一个恒成立的条件
$tj2 = " 1=1 ";
$tj3 = " 1=1 ";
$tj4 = " 1=1 ";
if(!empty($_GET["qy"])){ //区域的条件判断
$qy = $_GET["qy"];
$str = implode("','", $qy);
$tj1 = "area in ('{$str}')";
}
if(!empty($_GET["zflx"])){ //租房类型的条件判断
$zflx = $_GET["zflx"];
$str = implode("','", $zflx);
$tj2 = "renttype in ('{$str}')";
}
if(!empty($_GET["hx"])){ //户型的条件判断
$hx = $_GET["hx"];
$str = implode("','", $hx);
$tj3 = "housetype in ('{$str}')";
}
if(!empty($_GET["keyword"])){ //关键字查询的条件判断
$keyword = $_GET["keyword"];
$tj4 = "keyword like '%{$keyword}%'";
}
$zts = "select count(*) from housedb where {$tj1} and {$tj2} and {$tj3} and {$tj4}";
$ats = $db->query($zts,0);
$page = new page($ats[0][0],3); //分页查询取总数,设置每页显示的行数据
效果如图:
$sql = "select * from housedb where {$tj1} and {$tj2} and {$tj3} and {$tj4}".$page->limit; //利用拼接字符串方式将调取分页方法与条件进行拼接
$arr = $db->query($sql,0);
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>";
}
?>
</tbody>
</table>
效果如图:
<div>
<?php
echo $page->fpage();
?>
</div>
</div>
</form>
</body>
<script>
//使用JS实现全选功能
function qx(qx){
var ck1 = document.getElementsByClassName("ck1");
for(var i=0;i<ck1.length;i++){
ck1[i].checked=qx.checked;
}
}
function zflx(zflx){
var ck2 = document.getElementsByClassName("ck2");
for(var i=0;i<ck2.length;i++){
ck2[i].checked=zflx.checked;
}
}
function hx(hx){
var ck3 = document.getElementsByClassName("ck3");
for(var i=0;i<ck3.length;i++){
ck3[i].checked=hx.checked;
}
}
</script>
</html>
PHP连接数据库实现多条件查询与分页功能——关于租房页面的完整实例操作的更多相关文章
- SSM整合 mybatis多条件查询与分页
多条件查询与分页: 通过页面的houseName.floorage获取值传到前端视图(HouseSearchVO)实体类中的houseName,floorage建立houseSearchVO对象. 通 ...
- WebFrom 小程序【条件查询与分页整合】
将前面的条件查询功能与分页显示整合到一个页面中 <%@ Page Language="C#" AutoEventWireup="true" CodeFil ...
- TP条件查询和分页查询
一.条件查询 前端页面 <!doctype html> <html> <head> <meta charset="utf-8"> & ...
- Spring MVC和Spring Data JPA之按条件查询和分页(kkpaper分页组件)
推荐视频:尚硅谷Spring Data JPA视频教程,一学就会,百度一下就有, 后台代码:在DAO层继承Spring Data JPA的PagingAndSortingRepository接口实现的 ...
- spring data jpa实现多条件查询(分页和不分页)
目前的spring data jpa已经帮我们干了CRUD的大部分活了,但如果有些活它干不了(CrudRepository接口中没定义),那么只能由我们自己干了.这里要说的就是在它的框架里,如何实现自 ...
- (转)Entity Framework4.1实现动态多条件查询、分页和排序
原文:http://www.cnblogs.com/ahui/archive/2011/08/04/2127282.html EF通用的分页实现: 1 2 3 4 5 6 7 8 9 10 11 12 ...
- 【spring data jpa】带有条件的查询后分页和不带条件查询后分页实现
一.不带有动态条件的查询 分页的实现 实例代码: controller:返回的是Page<>对象 @Controller @RequestMapping(value = "/eg ...
- thinkphp5.0多条件模糊查询以及多条件查询带分页如何保留参数
1,多条件模糊查询 等于:map[‘id′]=array(‘eq′,100);不等于:map[‘id′]=array(‘eq′,100);不等于:map[‘id’] = array(‘neq’,100 ...
- Spring Boot Jpa 多条件查询+排序+分页
事情有点多,于是快一个月没写东西了,今天补上上次说的. JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将 ...
随机推荐
- Mysql 系统表
Information_schema: 1. tables 提供表信息: 表所属数据库,表名,表类型,行数,最大自增数等等.
- eclipse 设置
修改工作空间默认编码:Window --> Preferences --> General --> Workspace --> Text file encoding --> ...
- 前端JS Excel解析导入
本文转载自:https://www.cnblogs.com/yinqingvip/p/6743213.html 需要用到js-xlsx:下载地址:js-xlsx <!DOCTYPE html&g ...
- Django 路由报错友好提示
这个方法要在设置路由文件内使用也就是urls.py内. """mysite URL Configuration The `urlpatterns` list routes ...
- C connect实现Timeout效果(Linux)
C connect函数是阻塞的,现要实现非阻塞式的connect. int SocketClient::connectTimeOut(const int &connect_fd, const ...
- 导出为word文档
原来用freemarker就可以,真是太简便了.先设计一张文档,然后把要输出的值用freemarker取值表达式获取数据,最后保存为ftl文件,再调整一下就可以了.
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊(分块算法)
传送门 题意: 中文题意,不再赘述. 题解: 下午在补分块算法的相关知识,看到某大神博客推荐的这道题目,就试着做了做: TLE了一下午可还行: 我的思路: 将这 n 个点分成 sqrt(n) 块: i ...
- php的扩展配置
第一步:在php的安装目录下找到php.ini-development这个文件,并复制一个副本,修改这个副本的文件名为:php.ini 如下图我的目录为: 第二步:打开这个php.ini,修改 将这里 ...
- Druid 数据库连接池
druid 数据库连接池 由阿里提供 步骤 1 导包 durid1.0.9 jar 包 2 定义配置文件 必须是 properties文件 名字任意 位置也任意 3 获得数据库连接池对象 通过 Dur ...
- shell脚本[] [[]] -n -z 的含义解析
1.在中括号中,判断变量的值, 加不加双引号的问题?-z 判断 变量的值,是否为空: zero = 0 - 变量的值,为空,返回0,为true- 变量的值,非空,返回1,为false-n 判断变量的值 ...