1. 程式人生 > >php查詢

php查詢

input cin 條件查詢 php div for 技術分享 debug alt

查詢頁面代碼簡要說明: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查詢