租房子多条件查询练习
<!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="zf_duotiaojian.php" method="post"> <div>区域:<input type="checkbox" name="quanxuan" onclick="checkall(this,\'quyu\')">全选</div> <?php include("./lei/AAA.class.php"); $db=new AAA(); $sql="select distinct area from house "; $attr=$db->Query($sql); echo "<table>"; echo "<tr>"; foreach($attr as $v){ echo "<td><input type=\'checkbox\' name=\'quyu[]\' class=\'quyu\' value=\'$v[0]\'>{$v[0]}<td>"; } echo "</tr>"; echo "</table>"; ?> <br /> <div>租赁类型:<input type="checkbox" name="zllx" onclick="checkall(this,\'zllx\')">全选</div> <?php $sql="select distinct renttype from house "; $attr=$db->Query($sql); echo "<table>"; echo "<tr>"; foreach($attr as $v){ echo "<td><input type=\'checkbox\' name=\'zllx[]\' class=\'zllx\' value=\'$v[0]\'>{$v[0]}<td>"; } echo "</tr>"; echo "</table>"; ?> <br /> <div>房屋类型:<input type="checkbox" name="fwlx" onclick="checkall(this,\'fwlx\')">全选</div> <?php $sql="select distinct housetype from house "; $attr=$db->Query($sql); echo "<table>"; echo "<tr>"; foreach($attr as $v){ echo "<td><input type=\'checkbox\' name=\'fwlx[]\' class=\'fwlx\' value=\'$v[0]\'>{$v[0]}<td>"; } echo "</tr>"; echo "</table>"; ?> <br /> <div>关键字:<input type="text" name="guanjianzi"></div><br /> <input type="submit" value="搜索"> </form> <br /> <table width="100%" border="1" cellspacing="0" cellpadding="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[\'quyu\']) && count($_POST[\'quyu\'])>0) { $str=implode("\',\'",$_POST["quyu"]); $tj1=" area in(\'{$str}\') "; } //判断租赁类型 if(!empty($_POST["zllx"]) && count($_POST["zllx"])>0) { $str=implode("\',\'",$_POST["zllx"]); $tj2=" renttype in(\'{$str}\') "; } //判断房屋类型 if(!empty($_POST["fwlx"]) && count($_POST["fwlx"])>0) { $str=implode("\',\'",$_POST["fwlx"]); $tj3=" housetype in(\'{$str}\') "; } //判断关键字 if(!empty($_POST["guanjianzi"])) { $tj4=" keyword like \'%{$_POST[\'guanjianzi\']}%\'"; } $sall="select * from house where {$tj1} and {$tj2} and {$tj3} and {$tj4} "; $aall=$db->Query($sall); foreach($aall 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> <script type="text/javascript"> function checkall(d,a){ var ck=document.getElementsByClassName(a); for(var i=0;i<ck.length;i++) { if(d.checked){ ck[i].setAttribute("checked","checked"); } else{ ck[i].removeAttribute("checked"); } } } </script> </body> </html>
引用封装好的类
<?php class AAA { public $host="localhost"; public $uid="root"; public $pwd="123"; public $dbname="housedb"; /** *给一个sql语句,返回执行的结果 *@param string $sql 用户指定的sql语句 *@param int $type 用户给的语句类型,0代表增删改,1代表查询 *@return 返回查询的结果,如果是查询返回二维数组,如果是增删改返回true或false */ function Query($sql,$type=1) { //造连接对象 $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); //执行sql语句 $reslut = $db->query($sql); //从结果集对象里面取数据 if($type==1) { return $reslut->fetch_all(); } else { return $reslut; } } /** *给一个sql语句,返回关联的二维数组 *@param string $sql 用户指定的sql语句 *@param int $type 用户给的语句类型,0代表增删改,1代表查询 *@return 返回查询的结果,如果是查询返回二维数组,如果是增删改返回true或false */ function GuanQuery($sql,$type=1) { //造连接对象 $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); //执行sql语句 $reslut = $db->query($sql); //取数据 if($type==1) { $attr = array(); while($a = $reslut->fetch_assoc()) { $attr[] = $a; } return $attr; } else { return $reslut; } } /** *给一个sql语句,返回字符串 *@param string $sql 用户指定的sql语句 *@param int $type 用户给的语句类型,0代表增删改,1代表查询 *@return 返回查询的结果,如果是查询返回字符串,如果是增删改返回true或false */ function StrQuery($sql,$type=1) { //造连接对象 $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); //执行sql语句 $reslut = $db->query($sql); //取数据 if($type==1) { $attr = $reslut->fetch_all(); $str=""; foreach($attr as $v) { $str .= implode("^",$v); $str .="|"; } return substr($str,0,strlen($str)-1); } else { return $reslut; } } }
版权声明:本文为zxl89原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。