php数据访问-例题(租房子)

 

 

 

 

 

1.数据库建表

2. zufangzi.php

  1 <body>
  2 
  3 <h1>租房子</h1>
  4 
  5 <form action="zufangzi.php" method="post">
  6 <div>区域:<input type="checkbox" name="qx" onclick="quanxuan(this,'qy')" />全选</div>
  7 <div>
  8 <?php
  9 require "DBDA.class1.php";
 10 $db = new DBDA();
 11 
 12 $sqy = "select distinct area from house";//写SQL语句,并去重
 13 $aqy = $db->query($sqy);
 14 foreach($aqy as $v)
 15 {
 16     echo "<input type='checkbox' name='qy[]' value='{$v[0]}' class='qy' />{$v[0]}";
 17 }
 18 ?>
 19 </div>
 20 <br />
 21 
 22 <div>租赁类型:<input type="checkbox" name="zlqx" onclick="quanxuan(this,'zl')" />全选</div>
 23 <div>
 24 <?php
 25 $szl = "select distinct renttype from house";
 26 $azl = $db->query($szl);
 27 foreach($azl as $v)
 28 {
 29     echo "<input type='checkbox' name='zl[]' value='{$v[0]}' class='zl' />{$v[0]}";
 30 }
 31 ?>
 32 </div>
 33 <br />
 34 <div>房屋类型:<input type="checkbox" name="fwqx" onclick="quanxuan(this,'fw')" />全选</div>
 35 <div>
 36 <?php
 37 $sfw = "select distinct housetype from house";
 38 $afw = $db->query($sfw);
 39 foreach($afw as $v)
 40 {
 41     echo "<input type='checkbox' name='fw[]' value='{$v[0]}' class='fw' />{$v[0]}";
 42 }
 43 ?>
 44 </div>
 45 <br />
 46 <div>关键字:<input type="text" name="key" /> <input type="submit" value="查询" /></div>
 47 </form>
 48 <br />
 49 
 50 <table width="100%" border="1" cellpadding="0" cellspacing="0">
 51     <tr>
 52         <td>关键字</td>
 53         <td>区域</td>
 54         <td>建筑面积</td>
 55         <td>租金</td>
 56         <td>租赁类型</td>
 57         <td>房屋类型</td>
 58     </tr>
 59     <?php
 60     
 61     $tj1 = " 1=1 ";
 62     $tj2 = " 1=1 ";
 63     $tj3 = " 1=1 ";
 64     $tj4 = " 1=1 ";
 65     
 66     if(!empty($_POST["qy"]))
 67     {
 68         $aqy = $_POST["qy"];
 69         $sqy = implode("','",$aqy);
 70         
 71         $tj1 = " area in ('{$sqy}') ";
 72     }
 73     
 74     if(!empty($_POST["zl"]))
 75     {
 76         $azl = $_POST["zl"];
 77         $szl = implode("','",$azl);
 78         
 79         $tj2 = " renttype in ('{$szl}') ";
 80     }
 81     
 82     if(!empty($_POST["fw"]))
 83     {
 84         $afw = $_POST["fw"];
 85         $sfw = implode("','",$afw);
 86         
 87         $tj3 = " housetype in ('{$sfw}') ";
 88     }
 89     
 90     if(!empty($_POST["key"]))
 91     {
 92         $k = $_POST["key"];
 93         $tj4 = " keyword like '%{$k}%' ";
 94     }
 95     
 96     
 97     $sql = "select * from house where {$tj1} and {$tj2} and {$tj3} and {$tj4}";
 98     echo $sql;
 99     
100     $arr = $db->query($sql);
101     foreach($arr as $v)
102     {
103         echo "<tr>
104         <td>{$v[1]}</td>
105         <td>{$v[2]}</td>
106         <td>{$v[3]}</td>
107         <td>{$v[4]}</td>
108         <td>{$v[5]}</td>
109         <td>{$v[6]}</td>
110     </tr>";
111     }
112     ?>
113 </table>
114 
115 </body>
116 <script type="text/javascript">
117 function quanxuan(qx,a)
118 {
119     //找到该全选按钮对应的checkbox列表
120     var ck = document.getElementsByClassName(a);
121     //找全选按钮选中状态
122     if(qx.checked)
123     {
124         for(var i=0;i<ck.length;i++)
125         {
126             ck[i].setAttribute("checked","checked");
127         }
128     }
129     else
130     {
131         for(var i=0;i<ck.length;i++)
132         {
133             ck[i].removeAttribute("checked");
134         }
135     }
136     
137 }
138 </script>
139 </html>

 

所引用的封装类

 1 <?php
 2 class DBDA
 3 {
 4     public $host = "localhost";
 5     public $uid = "root";
 6     public $pwd = "123";
 7     public $dbname = "test_123";
 8     //执行SQL语句返回相应的结果
 9     //$sql 要执行的SQL语句
10     //$type 代表SQL语句的类型,0代表增删改,1代表查询
11     function query($sql,$type=1)
12     {
13         $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
14         
15         $result = $db->query($sql);
16         
17         if($type)
18         {
19             //如果是查询,显示数据
20             return $result->fetch_all();
21         }
22         else
23         {
24             //如果是增删改,返回true或者false
25             return $result;
26         }
27     }
28 }

 

呈现页面

 

posted @ 2017-05-05 15:03  ChrissZhao  阅读(234)  评论(0编辑  收藏  举报