PHP多条件查询 (租房查询)
Posted 张凯::get("新技能")
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP多条件查询 (租房查询)相关的知识,希望对你有一定的参考价值。
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 </head> 7 8 <body> 9 <?php 10 11 $db = new mysqli("localhost","root","123","housedb"); 12 13 $tj1 = " 1=1 "; 14 $tj2 = " 1=1 "; 15 $tj3 = " 1=1 "; 16 $tj4 = " 1=1 "; 17 18 //区域的条件 19 if(!empty($_POST["qx"]) && count($_POST["qx"])>0) 20 { 21 //$tj1 = ""; 22 $qx = $_POST["qx"]; 23 //code in(\'类型\',\'数量\',\'都好久\',\'队伍\',\'额\') 24 $str = implode("\',\'",$qx); 25 26 $tj1 = " area in(\'{$str}\') "; 27 } 28 //租赁类型的条件 29 if(!empty($_POST["zl"]) && count($_POST["zl"])>0) 30 { 31 //$tj1 = ""; 32 $zl = $_POST["zl"]; 33 //code in(\'类型\',\'数量\',\'都好久\',\'队伍\',\'额\') 34 $str = implode("\',\'",$zl); 35 36 $tj2 = " renttype in(\'{$str}\') "; 37 } 38 //房屋类型的条件 39 if(!empty($_POST["fw"]) && count($_POST["fw"])>0) 40 { 41 //$tj1 = ""; 42 $fw = $_POST["fw"]; 43 //code in(\'类型\',\'数量\',\'都好久\',\'队伍\',\'额\') 44 $str = implode("\',\'",$fw); 45 46 $tj3 = " housetype in(\'{$str}\') "; 47 } 48 //关键字的条件 49 if(!empty($_POST["key"])) 50 { 51 $key = $_POST["key"]; 52 $tj4 = " keyword like \'%{$key}%\' "; 53 } 54 55 ?> 56 57 <h1>租房子</h1> 58 59 <form action="test.php" method="post"> 60 61 <div>区域:<input type="checkbox" onclick="checkall(this)" />全选</div> 62 <div> 63 <?php 64 $sqlq = "select distinct area from house"; 65 $rq = $db->query($sqlq); 66 $aq = $rq->fetch_all(); 67 foreach($aq as $v) 68 { 69 echo "<input type=\'checkbox\' name=\'qx[]\' value=\'{$v[0]}\' class=\'qxlist\' />{$v[0]} "; 70 } 71 ?> 72 </div> 73 <br /> 74 <div>租赁类型:<input type="checkbox" />全选</div> 75 <div> 76 <?php 77 $sqlz = "select distinct renttype from house"; 78 $rz = $db->query($sqlz); 79 $az = $rz->fetch_all(); 80 foreach($az as $v) 81 { 82 echo "<input type=\'checkbox\' name=\'zl[]\' value=\'{$v[0]}\' />{$v[0]} "; 83 } 84 ?> 85 </div> 86 <br /> 87 <div>房屋类型:<input type="checkbox" />全选</div> 88 <div> 89 <?php 90 $sqlf = "select distinct housetype from house"; 91 $rf = $db->query($sqlf); 92 $af = $rf->fetch_all(); 93 foreach($af as $v) 94 { 95 echo "<input type=\'checkbox\' name=\'fw[]\' value=\'{$v[0]}\' />{$v[0]} "; 96 } 97 ?> 98 </div> 99 <br /> 100 <div>关键字:<input type="text" name="key" /> 101 <input type="submit" value="查询" /> 102 </div> 103 <br /> 104 105 </form> 106 <table width="100%" border="1" cellpadding="0" cellspacing="0"> 107 <tr> 108 <td>关键字</td> 109 <td>区域</td> 110 <td>面积</td> 111 <td>租金</td> 112 <td>租赁类型</td> 113 <td>房屋类型</td> 114 </tr> 115 <?php 116 117 $sqlall = "select * from house where {$tj1} and {$tj2} and {$tj3} and {$tj4}"; 118 $rall = $db->query($sqlall); 119 $aall = $rall->fetch_all(); 120 foreach($aall as $v) 121 { 122 echo "<tr> 123 <td>{$v[1]}</td> 124 <td>{$v[2]}</td> 125 <td>{$v[3]}</td> 126 <td>{$v[4]}</td> 127 <td>{$v[5]}</td> 128 <td>{$v[6]}</td> 129 </tr>"; 130 } 131 ?> 132 133 </table> 134 135 <script type="text/javascript"> 136 137 function checkall(a) 138 { 139 var ck = document.getElementsByClassName("qxlist"); 140 //注意checked 的属性 无论题中有没有写 都有默认值 所以 可以写城下面 方式 其他 以此类推 其他的的如果 里面没有的属性(比如 a 自己定义的 需要 attribute() 属性添加) 141 if(a.checked) 142 { 143 for(var i=0;i<ck.length;i++) 144 { 145 /*ck[i].setAttribute("checked","checked");*/ 146 ck[i].checked="checked"; 147 } 148 } 149 else 150 { 151 for(var i=0;i<ck.length;i++) 152 { 153 /*ck[i].removeAttribute("checked");*/ 154 ck[i].checked=""; 155 } 156 } 157 } 158 159 </script> 160 </body> 161 </html>
表单内 不需要传的值 把name 名 取消掉 就可以
1 1. <form> 表单忘记 写地址和 传输方法 2 2.忘记写$符号 3 3.name名 没有加[](只有在加[]时才可以 以数组的形式传值) 4 接受name 名的时候 以 引号内部 不加 [] 详细见例题 5 4.传值 解析 最终 就是字符串拼接成 一个语句 语句要完全和 正常书写的内容一样 6 5. 下面练习时 出现的一个错误 引用 上4类型 查询语句中 解析出来后 where和{$tj} 解析出的内容之间没有空格 所以输出错误 7 6. mysql 查询语句的时候 语句写完 不能与分号之间不能留空格 8 9 10 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 11 <html xmlns="http://www.w3.org/1999/xhtml"> 12 <head> 13 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 14 <title>无标题文档</title> 15 </head> 16 <?php 17 $lianjei=new MySQLi("localhost","root","123","housedb"); 18 ?> 19 <form action="zufangpractice.php" method="post"> 20 <div> 地区<input type="checkbox" onclick="hh(this,\'a1\')" >全选</div> 21 <?php 22 $Area="select distinct Area from house"; 23 $area1=$lianjei->query($Area); 24 $area2=$area1->fetch_all(); 25 foreach($area2 as $a1) 26 { 27 echo "<input name=\'qx[]\' type=\'checkbox\' value=\'{$a1[0]}\' class=\'a1\'/>{$a1[0]}"; 28 } 29 ?> 30 31 32 <div>出租类型 <input type="checkbox" onclick="hh(this,\'a2\')" >全选</div> 33 <?php 34 $Area="select distinct renttype from house"; 35 $area1=$lianjei->query($Area); 36 $sm=$area1->fetch_all(); 37 foreach($sm as $a2) 38 { 39 echo "<input name=\'rt[]\' type=\'checkbox\' value=\'{$a2[0]}\' class=\'a2\'/>{$a2[0]}"; 40 } 41 ?> 42 43 44 <div>居住类型 <input type="checkbox" onclick="hh(this,\'a3\')" >全选</div> 45 <?php 46 $Area="select distinct housetype from house"; 47 $area1=$lianjei->query($Area); 48 $area2=$area1->fetch_all(); 49 foreach($area2 as $a3) 50 { 51 echo "<input name=\'ht[]\' type=\'checkbox\' value=\'{$a3[0]}\' class=\'a3\'/>{$a3[0]}"; 52 } 53 ?> 54 55 <div > <input type="text" name="mohu"></div> 56 <div><input type="submit" value="查询"></div> 57 </form> 58 59 </form> 60 <table width="100%" cellpadding="0" cellspacing="0" border="1"> 61 <tr> 62 <td>编辑</td> 63 <td>删除</td> 64 <td>关键字</td> 65 <td>区域</td> 66 <td>建筑面积</td> 67 <td>租金</td> 68 <td>租赁类型</td> 69 <td>房屋类型</td> 70 </tr> 71 72 73 74 <?php 75 76 77 78 79 $name=\'\'; 80 $tj="1=1"; 81 $tj1="1=1"; 82 $tj2="1=1"; 83 $tj3="1=1"; 84 if(!empty($_POST["qx"]) && count($_POST["qx"])>0) 85 { 86 //$tj1 = ""; 87 $qx = $_POST["qx"]; 88 //code in(\'类型\',\'数量\',\'都好久\',\'队伍\',\'额\') 89 $str = implode("\',\'",$qx); 90 $tj1 = " area in(\'{$str}\') "; 91 } 92 if(!empty($_POST["rt"])) 93 { 94 $qx = $_POST["rt"]; 95 $str = implode("\',\'",$qx); 96 $tj2 = " renttype in(\'{$str}\') "; 97 } 98 if(!empty($_POST["ht"])) 99 { 100 $qx = $_POST["ht"]; 101 $str = implode("\',\'",$qx); 102 $tj3 = " housetype in(\'{$str}\') "; 103 } 104 105 106 107 if(!empty($_POST["mohu"])) 108 { 109 $name=$_POST["mohu"]; 110 $tj=" keyword like \'%{$name}%\' "; 111 112 } 113 var_dump($tj3); 114 $lianjie=new MySQLi(\'localhost\',\'root\',\'123\',\'housedb\'); 115 mysqli_connect_error()?die("链接错误"):\'\'; 116 $zhunbei=" select * from house where {$tj1} and {$tj2} and {$tj3} and {$tj}"; 117 118 $zhixing=$lianjie->query($zhunbei); 119 $shuzu=$zhixing->fetch_all(); 120 foreach($shuzu as $v) 121 { 122 123 $str=str_replace($name,"<mark>{$name}</mark>",$v[1]); 124 echo "<tr><td><a href=\'bianji.php?f=$v[0]\'>编辑</a></td> 125 <td><a href=\'shanchu.php?f=$v[0]\' onclick=\\" return confirm(\'确定要删除么\')\\">删除</a></ 126 127 128 td> <td>$str</td> <td>$v[2]</td> <td>$v[3]</td> <td>$v[4]</td> <td>$v[5]</td> <td>$v[6]</td></tr>"; 129 130 } 131 132 ?> 133 134 </table> 135 136 137 <body> 138 <script> 139 function hh(a,b) 140 { 141 var t=document.getElementsByClassName(b); 142 if(a.checked) 143 { for(var i=0;i<t.length;i++) 144 { 145 t[i].checked="checked"; 146 } 147 } 148 else 149 { 150 for(var i=0;i<t.length;i++) 151 { 152 t[i].checked=""; 153 } 154 } 155 156 157 } 158 </script> 159 </body> 160 </html>
<!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> <div style="background-color:#F00; border-radius:100px"></div> <body> <div class="bbb" style="width:200px; height:50px"></div> <form action="tp.php" method="post"> <?php $lianjie=new MySQLi("localhost","root","123","tp"); $zhunbei="select distinct name,bfs,shu from piao ";//这里不要单独取某个数据 要取* (取所有的 数据) $zhixing = $lianjie->query($zhunbei); $shuzu =$zhixing->fetch_all(); foreach($shuzu as $v) { echo "<div style=\'width:300px; height:40px; \'> <div style=\' width:100px; height:40px; text-align:center; float:left;\'><input type=\'checkbox\' name=\'a[]\' value=\'{$v[0]}\'/>{$v[0]} </div> <div class=\'bbb\' style=\'width:190px; height:40px; display:none ; float:right\'> <div style=\'width:190px; height:10px;border-radius:100px; border:1px solid blue; float:right\'> <div style=\'width:{$v[1]}; height:10px; background-color:#F00; border-radius:100px\'></div> </div> <div style=\'width:190px; height:20px; float:right\'>{$v[1]}<div style=\'width:50px; float:right\'>{$v[2]}票</div></div> </div> </div>"; } ?> <div><input onclick="xiaoshi()" type="submit" value="提交"><input onclick="chaxun()" type="button" value="查看"></div> <form> </body> <?php if(!empty($_POST["a"])) { $h=$_POST["a"]; $f=implode("\',\'",$h); $update=" update piao set shu=shu+1 where name in(\'{$f}\'); "; $zhixing =$lianjie->query($update); $sum="select shu from piao"; $zhixing2=$lianjie->query($sum); $jieguo=$zhixing2->fetch_all(); $he=0;//求和的地方这里可以用 sum()方法 sum(栏位名); 取出1个二维数组 取[0][0] foreach($jieguo as $hh) { $he+=$hh[0]; } foreach($jieguo as $gg) { $r=$gg[0]/$he; $t= number_format($r*100,2);//取小数点后面2位 $updat=" update piao set bfs=\'{$t}%\' where shu=$gg[0]; "; $zhixing3 =$lianjie->query($updat); } } ?> <script type="text/javascript"> //如果是 JS 按钮 不能是 提交按钮 一提交就刷新啊!!!! /*怎么建立数据库 不熟练 难点是 建立一个 合适的数据库 ; 调用和填写数据 注意 数据库可以做到尽量充实 只是 小部分展示给客户 这样做起题来方便 做题中 考验数据存取 提取一个数据 和数个数据 其他的 都可以作为参数 传递 不要 思维固定 充分利用数据库 */ function chaxun() { var bbb=document.getElementsByClassName("bbb"); for(var i=0;i<bbb.length;i++) { bbb[i].style.display="block"; } } </script> </html>
以下两个 代码框 对投票问题 重新写(封装类)【封装类是新知识注意看】
投票1:
<!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> <?php include("./DBDA.class.php"); $db = new DBDA(); $attr = $_POST["xx"]; foreach($attr as $v) { $sql = "update diaoyanxuanxiang set numbers=numbers+1 where ids=\'{$v}\'"; $db->Query($sql,0); } //显示结果 $sql = "select * from diaoyanxuanxiang"; $shuzu = $db->Query($sql); $szs = "select sum(numbers) from diaoyanxuanxiang"; $azs = $db->Query($szs); $zs = $azs[0][0]; foreach($shuzu as $v) { $bfb = ($v[2]/$zs)*100; echo "{$v[1]}:{$v[2]}({$bfb}%)<div style=\'width:200px;height:10px; border:1px solid red;\'><div style=\'background-color:green; width:{$bfb}%; height:10px\'></div></div>"; } ?> </body> </html>
投票: 封装类
1 <?php 2 class DBDA 3 { 4 public $host="localhost"; 5 public $uid = "root"; 6 public $pwd = "123"; 7 public $dbname = "mydb"; 8 9 //成员方法 PHP连接数据库实现多条件查询与分页功能——关于租房页面的完整实例操作