2012-10-22 70 views
-3

我是比較新的PHP。我有多個下拉列表用於查詢數據庫中的表。我想要做的是使每個下拉列表中的第一個項目在每個下拉列表中的「全選」列表項目中搜索該表格以查找該特定列表中的所有其他項目。PHP下拉「選擇所有」選項來搜索和顯示來自MySQL的結果分貝

Here's my HTML: 
<form name="campsite_search" action="filterprop.php" method="get"> 
    <select name="rent_sale"> 
     <option value="" selected="selected">For Rent/Sale</option> 
     <option value="rent">For Rent</option> 
     <option value="sale">For Sale</option> 
    </select> 
    <select name="location"> 
     <option value="" selected="selected">All locations</option> 
     <option value="gaborone">Gaborone</option> 
     <option value="francistown">Francistown</option> 
    </select> 
    <select name="housetype"> 
     <option value="" selected="selected">All House Types</option> 
     <option value="apartment">Apartment</option> 
     <option value="double">Double-storey</option> 
    </select> 
    <select name="bedrooms"> 
     <option value="" selected="selected">No. of bedrooms</option> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="3">3</option> 
    </select> 
    <select name="ensuite"> 
     <option value="" selected="selected">No. of bedrooms ensuite</option> 
     <option value="0">0</option> 
     <option value="1">1</option> 
     <option value="2">3</option> 
    </select> 
    <select name="servantsq"> 
     <option value="" selected="selected">Servants Quarter</option> 
     <option value="No">No</option> 
     <option value="Yes">Yes</option> 
    </select> 
    </select> 
     <select name="otherfeatures"> 
     <option value="" selected="selected">Other Features</option> 
     <option value="pool">Swimming Pool</option> 
     <option value="garage">Garage</option> 
     <option value="balcony">Balcony</option> 
    </select> 

<input type="submit" name="submit"value="Continue" /> 
</form> 

我的PHP:

<?php 

$con = mysql_connect ("localhost","root",""); 
if (!$con){ 
die('Could not connect:' .mysql_error()); 
} 
mysql_select_db("my_db", $con); 

      $rent_sale = $_GET['rent_sale']; 
      $location = $_GET['location']; 
      $housetype = $_GET['housetype']; 
      $bedrooms = $_GET['bedrooms']; 
      $ensuite = $_GET['ensuite']; 
      $servantsq = $_GET['servantsq']; 
      $otherfeatures = $_GET['otherfeatures']; 

$mysql_query = "SELECT * FROM property WHERE rent_sale='$rent_sale' AND location='$location' AND housetype='$housetype' AND bedrooms='$bedrooms' AND ensuite='$ensuite' AND servantsq='$servantsq' AND otherfeatures='$otherfeatures'"; 

$result = mysql_query($mysql_query) or die (mysql_error()); 

while($row = mysql_fetch_array($result)){ 
echo $row['location']; 
} 
mysql_close($con); 
?> 

到目前爲止它查詢我的表,只顯示位置(這是確定現在),但如果我不選擇一個項目這是行不通的每一個下拉列表,這就是爲什麼我想要第一個項目選擇所有類別...

在此先感謝球員。

+1

好的,它可以有幾個含義。首先,你的數據庫是你想要選擇的領域嗎? (所以這些字段有一個非空)也可能是你試圖標記的值。你可以創造一個價值,這意味着沒有選擇什麼東西,就像沒有。所以當它沒有,數據庫中有東西,但它不會與你的查詢 – Dorvalla

+0

對應如果我可以說......最後的選擇..你只能選擇陽臺或游泳池或車庫?你可能想製作這些選擇框。有人想要所有三個;) – Dorvalla

回答

0
$rent_sale = if(isset($_GET['rent_sale'])){$_GET['rent_sale']}else{'*'}; 

$rent_sale = if($_GET['rent_sale'] != ""){$_GET['rent_sale']}else{'*'}; 

其中這兩個應該都是正常的,我不知道,如果你的代碼將返回空或不是,如果它的第一部作品,如果沒有第二個應該。你顯然必須爲每個變量做這件事。

相關問題