php查询

Posted FTHeD

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php查询相关的知识,希望对你有一定的参考价值。

查询页面代码简要说明:14-15为加载DBDA页面;16-41为判断选项或关键字查询是否为非空,判断从form表单传来的值是否为非空,把字符串进行拼接
  1 <title>无标题文档</title>
  2 <style type="text/css">
  3 .wai{ width:100%; height:50px;}
  4 .a{ width:100px; height:30px; float:left}
  5 .nei{ width:100%; height:50px;}
  6 .b{ width:100px; height:30px; float:left}
  7 .c{ width:100px; height:30px; float:left}
  8 .nw{ width:100%; height:50px;}
  9 </style>
 10 </head>
 11 
 12 <body>
 13 <?php 
 14     require_once "./DBDA.class.php";
 15     $db = new DBDA();
 16     $tj1 = " 1=1 ";
 17         $tj2 = " 1=1 ";
 18         $tj3 = " 1=1 ";
 19         $tj4 = " 1=1 ";
 20         $arr1 = array();
 21         if(!empty($_POST["qy"])){
 22             $arr1 = $_POST["qy"];
 23             $str = implode("\',\'",$arr1);
 24             $tj1 = "area in (\'{$str}\')";
 25             }
 26             $arr2 =array();
 27         if(!empty($_POST["zl"])){
 28             $arr2 =$_POST["zl"];
 29             $str = implode("\',\'",$arr2);
 30             $tj2 = "renttype in (\'{$str}\')";
 31             };
 32             $arr3 = array();
 33         if(!empty($_POST["fw"])){
 34             $arr3 = $_POST["fw"];
 35             $str = implode("\',\'",$arr3);
 36             $tj3 = "housetype in (\'{$str}\')";
 37             }
 38         if(!empty($_POST["key"])){
 39             $key = $_POST["key"];
 40             $tj4 = " keyword like \'%{$key}%\' ";
 41             }
 42     ?>
 43 <form action="chaxunhouse.php" method="post">
 44 <div class="wai">
 45 区域:<input type="checkbox" id="qy"/>全选<br />
 46 <div>
 47  <?php
 48     $sqlqy = "select distinct area from house";
 49     $arr = $db->query($sqlqy);
 50     foreach($arr as $v){
 51         if(in_array($v[0],$arr1)){
 52             echo "<div class=\'a\'><input type=\'checkbox\' name=\'qy[]\' value=\'{$v[0]}\' class=\'qy\' checked=\'checked\'>{$v[0]}</div>";
 53             }else{
 54         echo "<div class=\'a\'><input type=\'checkbox\' name=\'qy[]\' value=\'{$v[0]}\' class=\'qy\'>{$v[0]}</div>
 55                   
 56         ";}
 57         }
 58  ?></div></div>
 59  <div class="nw">
 60 租赁类型:<input type="checkbox" id="zl"/>全选<br />
 61 <div>
 62 <?php 
 63     $sqlzl = "select distinct renttype from house";
 64     $arrzl = $db->query($sqlzl);
 65     foreach($arrzl as $v){
 66         if(in_array($v[0],$arr2)){
 67             echo "<div class=\'c\'><input type=\'checkbox\' name=\'zl[]\' value=\'{$v[0]}\' class=\'zl\' checked=\'checked\'>{$v[0]}</div>}";}
 68         else{echo "<div class=\'c\'><input type=\'checkbox\' name=\'zl[]\' value=\'{$v[0]}\' class=\'zl\'>{$v[0]}</div>
 69                   
 70         ";}
 71         }
 72  ?></div></div>
 73  <div class="nei">
 74 房屋类型:<input type="checkbox" id="fw"/>全选<br />
 75 <div>
 76 <?php 
 77     $sqlfw = "select distinct housetype from house";
 78     $arrfw = $db->query($sqlfw);
 79     foreach($arrfw as $v){
 80         if(in_array($v[0],$arr3)){
 81             echo "<div class=\'b\'><input type=\'checkbox\' name=\'fw[]\' value=\'{$v[0]}\' class=\'fw\' checked=\'checked\'>{$v[0]}</div>";
 82             }else{
 83         echo "<div class=\'b\'><input type=\'checkbox\' name=\'fw[]\' value=\'{$v[0]}\' class=\'fw\'>{$v[0]}</div>
 84                   
 85         ";}
 86         }
 87  ?></div></div>
 88  <input type="submit" value="查询" />
 89  <input type="text" name="key"/>
 90  </form>
 91 <table border="1" cellpadding="0" cellspacing="1">
 92     <tr>
 93         <td>关键字</td>
 94         <td>区域</td>
 95         <td>使用面积</td>
 96         <td>租金</td>
 97         <td>租赁类型</td>
 98         <td>房屋类型</td>
 99     </tr>
100     <?php
101         $sql = "select * from house where {$tj1} and {$tj2} and {$tj3} and {$tj4}";
102         $arr = $db->query($sql);
103         var_dump($sql);
104         foreach($arr as $v){
105             echo "<tr>
106         <td>{$v[1]}</td>
107         <td>{$v[2]}</td>
108         <td>{$v[3]}</td>
109         <td>{$v[4]}</td>
110         <td>{$v[5]}</td>
111         <td>{$v[6]}</td>
112     </tr>";
113         }
114     ?>
115 </table>  
116 </body>

43-90的页面显示达到的效果

91-102的页面显示效果,通过sql语句实现查询的效果

$sql= "select * from house"改为条件查询字符串拼接即

\'select * from house where area in (\'锦绣园\') and renttype in (\'合租\') and housetype in (\'三室一厅\') and  keyword like \'%淄川%\' \' 
$sql = "select * from house where {$tj1} and {$tj2} and {$tj3} and {$tj4}";{$tj1}是通过代码16-41来获得

以上是关于php查询的主要内容,如果未能解决你的问题,请参考以下文章

PHP必用代码片段

PHP代码-psysh调试代码片段工具

超实用的php代码片段

比较有用的php代码片段

Wordpress查询从文章/页面中提取片段

php Yoast SEO规范输出的代码片段