首页 技术 正文
技术 2022年11月10日
0 收藏 919 点赞 3,008 浏览 2697 个字

第一个页面DBDA.class.php

<?php
class DBDA
{
public $host = "localhost";
public $uid = "root";
public $pwd = "root";
public $dbname = "dbname"; public function Query($sql,$type=1)//两个参数,第二个参数代表默认值
{
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$result = $db->query($sql); if($type=="1")//定义1类型的是查询,其它类型是增删改
{
return $result->fetch_all();
}else
{
return $result;
}
}
}

第二个页面main.php

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><body>
<h1>租房子</h1>
<form action="main.php" method="post">
<div>
<div>区域:<input type="checkbox" onclick="xuan(this,'qx')" />全选</div>//传入两个参数
<div>
<?php
include("./DBDA.class.php");//引入类文件
$db = new DBDA();
$sqx = "select distinct area from housedb";
$aqx = $db->Query($sqx); foreach($aqx as $v)
{
echo "<input type='checkbox' name='qx[]' class='qx' value='{$v[0]}' />{$v[0]}";
}
?>
</div>
</div>
<br />
<div>
<div>租赁类型:<input type="checkbox" onclick="xuan(this,'zl')" />全选</div>
<div>
<?php
$szl = "select distinct renttype from housedb";
$azl = $db->Query($szl); foreach($azl as $v)
{
echo "<input type='checkbox' name='zl[]' class='zl' value='{$v[0]}' />{$v[0]}";
}
?>
</div>
</div>
<br />
<div>
<div>房屋类型:<input type="checkbox" onclick="xuan(this,'fw')" />全选</div>
<div>
<?php
$sfw = "select distinct housetype from housedb";
$afw = $db->Query($sfw); foreach($afw as $v)
{
echo "<input type='checkbox' name='fw[]' class='fw' value='{$v[0]}' />{$v[0]}";
}
?>
</div>
</div>
<br />
<div>关键字:<input type="text" name="keywords" />
<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 $tj1 = " 1=1 ";
$tj2 = " 1=1 ";
$tj3 = " 1=1 ";
$tj4 = " 1=1 "; if(!empty($_POST["qx"]) && count($_POST["qx"])>0)
{
$str = implode("','",$_POST["qx"]);
$tj1 = " area in ('{$str}') ";
}
if(!empty($_POST["zl"]) && count($_POST["zl"])>0)
{
$str = implode("','",$_POST["zl"]);
$tj2 = " renttype in ('{$str}') ";
}
if(!empty($_POST["fw"]) && count($_POST["fw"])>0)
{
$str = implode("','",$_POST["fw"]);
$tj3 = " housetype in ('{$str}') ";
}
if(!empty($_POST["keywords"]))
{
$key = $_POST["keywords"];
$tj4 = " keyword like '%{$key}%' ";
} $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">
function xuan(qx,lei)//方法类似的尽量写一个,改变参数
{
var ck = document.getElementsByClassName(lei);
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");
}
}
}
</script>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294