1. 程式人生 > >數據訪問查詢實例 租房子

數據訪問查詢實例 租房子

all 狀態 word 判斷 ++ 數據 checked order sele

<body>
	<h2>租賃信息</h2><br />
	<div>
    <form action="zufang.php" method="post">
    <div>區       域:
    <input type="checkbox" onclick="CheckAll(this,‘qy‘)" />全選</div>
<?php
	require_once "DBDB.class.php";
	$db = new DBDB();
	 
	$sqlqy = "select distinct Area from house";
	$attrqy = $db->Query($sqlqy);
	//var_dump($attrqy);
	 
	foreach($attrqy as $v)
	{
		echo "<input class=‘qy‘ type=‘checkbox‘ value=‘{$v[0]}‘ name=‘qy[]‘/>{$v[0]} ";     
	}
?>
    
    <div><br />租賃類型:<input type="checkbox" onclick="CheckAll(this,‘zl‘)"/>全選</div>
    <div>
<?php
 
	$sqlzl = "select distinct RentType from House";
	$attrzl = $db->Query($sqlzl);
	 
	foreach($attrzl as $v)
	{
		echo "<input class=‘zl‘ type=‘checkbox‘ value=‘{$v[0]}‘ name=‘zl[]‘/>{$v[0]} ";     
	}
?>
    </div><br />
     
    <div>房屋類型:<input type="checkbox" onclick="CheckAll(this,‘fw‘)"  />全選</div>
    <div>
<?php
 
	$sqlfw = "select distinct HouseType from House";
	$attrfw = $db->Query($sqlfw);
	//var_dump($attrqy);
	 
	foreach($attrfw as $v)
	{
		echo "<input class=‘fw‘ type=‘checkbox‘ value=‘{$v[0]}‘ name=‘fw[]‘/>{$v[0]} ";     
	}
?>
    </div><br />
     
    <div >關  鍵  字:
    <input type="text" name="keyword" id="key" />
    <br />
    <input type="submit" value="搜索" />
    </form>
 
    <br />
    <br />
    <br />
 
    <table  border="2" width="80%" style="text-align:center;" >
    <tr>
        <td>關鍵字</td>
        <td>區域</td>
        <td>面積</td>
        <td>租金</td>
        <td>租賃類型</td>
        <td>房屋類型</td>
    </tr>
 
<?php
 
	$tj = "";
	$tj1 = " 1=1 ";//條件恒成立註意空格
	$tj2 = " 1=1 ";
	$tj3 = " 1=1 ";
	$tj4 = " 1=1 ";
	 
	if(!empty($_POST["qy"]))
	{
         // area in (‘‘,‘‘,‘‘,‘‘) $attr = $_POST["qy"]; $str = implode("‘,‘",$attr);//拼接字符串 $tj1 = " Area in (‘{$str}‘)"; } if(!empty($_POST["zl"])) { $attr = $_POST["zl"]; $str = implode("‘,‘",$attr); $tj2 = " RentType in (‘{$str}‘)"; } if(!empty($_POST["fw"])) { $attr = $_POST["fw"]; $str = implode("‘,‘",$attr); $tj3 = " HouseType in (‘{$str}‘)"; } if(!empty($_POST["keyword"])) { $attr = $_POST["keyword"]; $tj3 = " keyword like ‘%{$attr}%‘"; } $tj = " Where {$tj1} and {$tj2} and {$tj3} and {$tj4}";//Where 前加空格 $sql = "select * from House".$tj;//拼接 $attrall = $db->Query($sql); //var_dump($attrall); foreach($attrall 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> </div> <script type="text/javascript"> function CheckAll(a,b)//this表示該 { var qx = a.checked; var ck = document.getElementsByClassName(b); for(var i =0;i<ck.length;i++) { ck[i].checked = qx; } } </script> </body>

技術分享圖片

保持選中查詢後條件還是在選中狀態

<body>
	<h2>租賃信息</h2><br />
	<div>
    <form action="zufang.php" method="post">
    <div>區       域:
    <input type="checkbox" onclick="CheckAll(this,‘qy‘)" />全選</div>
<?php
	require_once "DBDB.class.php";
	$db = new DBDB();
	$tj = "";
	$tj1 = "1=1";
	$tj2 = "1=1";
	$tj3 = "1=1";
	$tj4 = "1=1";
	$attr1 = array(); 
	if(!empty($_POST["qy"]))
	{
		$attr1 = $_POST["qy"];
		$str = implode("‘,‘",$attr1);
		$tj1 = " Area in (‘{$str}‘)";  
	}
	$attr2 = array(); 
	if(!empty($_POST["zl"]))
	{
		$attr2 = $_POST["zl"];
		$str = implode("‘,‘",$attr2);
		$tj2 = " RentType in (‘{$str}‘)";  
	}
	$attr3 = array(); 
	if(!empty($_POST["fw"]))
	{
		$attr3 = $_POST["fw"];
		$str = implode("‘,‘",$attr3);
		$tj3 = " HouseType in (‘{$str}‘)"; 
	}
	$attr4 = "";  
	if(!empty($_POST["keyword"]))
	{
		$attr4 = $_POST["keyword"];
		$tj3 = " keyword like ‘%{$attr4}%‘";
	}
	 //把if語句拿到上面  拿到復選框的上面,把每個attr拿到花括號外面
	$sqlqy = "select distinct Area from house";
	$attrqy = $db->Query($sqlqy);
	//var_dump($attrqy);
	 
	foreach($attrqy as $v)//加if語句
	{
		if(in_array($v[0],$attr1)){
		//判斷$v[0]	是否在數組attr1中  遍歷出的$v[0]是否被選中
		echo "<input class=‘qy‘ type=‘checkbox‘ checked=‘checked‘ value=‘{$v[0]}‘ name=‘qy[]‘/>{$v[0]} ";
		}else{
			echo "<input class=‘qy‘ type=‘checkbox‘ value=‘{$v[0]}‘ name=‘qy[]‘/>{$v[0]} ";  	
		}   
	}
?>
    
    <div><br />租賃類型:<input type="checkbox" onclick="CheckAll(this,‘zl‘)"/>全選</div>
    <div>
<?php
 
	$sqlzl = "select distinct RentType from House";
	$attrzl = $db->Query($sqlzl);
	 
	foreach($attrzl as $v)
	{
		if(in_array($v[0],$attr2)){
			echo "<input class=‘zl‘ type=‘checkbox‘ checked=‘checked‘ value=‘{$v[0]}‘ name=‘zl[]‘/>{$v[0]} ";	
		}else{
			echo "<input class=‘zl‘ type=‘checkbox‘ value=‘{$v[0]}‘ name=‘zl[]‘/>{$v[0]} ";
		} 
	}
?>
    </div><br />
     
    <div>房屋類型:<input type="checkbox" onclick="CheckAll(this,‘fw‘)"  />全選</div>
    <div>
<?php
 
	$sqlfw = "select distinct HouseType from House";
	$attrfw = $db->Query($sqlfw);
	//var_dump($attrqy);
	 
	foreach($attrfw as $v)
	{
		if(in_array($v[0],$attr3)){
			echo "<input class=‘fw‘ type=‘checkbox‘ checked=‘checked‘ value=‘{$v[0]}‘ name=‘fw[]‘/>{$v[0]}";  	
		}else{
			echo "<input class=‘fw‘ type=‘checkbox‘ value=‘{$v[0]}‘ name=‘fw[]‘/>{$v[0]}";
		}
		   
	}
?>
    </div><br />
     
    <div >關  鍵  字:
    <input type="text" name="keyword" id="key" value=‘<?php echo $attr4; ?>‘/><!--加入一小段php代碼-->
    <br />
    <input type="submit" value="搜索"/>
    </form>
 
    <br />
    <br />
    <br />
 
    <table  border="2" width="80%" style="text-align:center;" >
    <tr>
        <td>關鍵字</td>
        <td>區域</td>
        <td>面積</td>
        <td>租金</td>
        <td>租賃類型</td>
        <td>房屋類型</td>
    </tr>
 
<?php
	$tj = " Where {$tj1} and {$tj2} and {$tj3} and {$tj4}";//Where  前加空格
	 
	$sql = "select * from House".$tj;
	$attrall = $db->Query($sql);
	//var_dump($attrall);
	 
	foreach($attrall 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>
    </div>

<script type="text/javascript">
	function CheckAll(a,b)//this表示該
	{
		var qx = a.checked;
		var ck = document.getElementsByClassName(b);
		for(var i =0;i<ck.length;i++)
		{
			ck[i].checked = qx;
		}
	}
	 
</script>
</body>

技術分享圖片

查詢完成條件還是選中狀態 和第一個不一樣 第一個查詢後條件就變為空

數據訪問查詢實例 租房子