我想在選擇選項菜單上創建一個篩選器選項,但我似乎無法使其工作。 我的過濾器的目標是過濾「stad」上的數據。通過選擇選項菜單篩選SQL數據HTML - PHP
表(回族):
id = int(2) primary key Auto_increment
stad = varchar(25)
stad means city in the Dutch Language
-------------------------
| id | .....| stad |
| 1 |......| Amsterdam |
| 2 |......| London |
-------------------------
哪裏.....是其他列。
我在while循環中循環選項菜單,所以當有一個新的「stad」註冊時,它會自動添加一個新選項。 我是否必須使用按鈕才能接收選定的選項並過濾顯示的數據?
我的PHP文件:
<form action="" method="post" name="filter">
<select class="filtermenu">
<option selected="true" disabled="disabled">Plaatsnaam</option>
<?php while($overzichtHuis = mysqli_fetch_assoc($overzichtRes)): ?>
<option value="<?php echo $overzichtHuis['stad']?>"><?php echo $overzichtHuis['stad']?></option>
<?php endwhile; ?>
</select>
<input type="submit" value="Filter toepassen" onclick="insert()">
</form>
//SQL Query
$overzichtData = "SELECT * FROM huis";
$overzichtRes = mysqli_query($con, $overzichtData) or die(mysqli_error($con));
$detailsData = "SELECT * FROM huis ORDER BY id DESC";
$detailsRes = mysqli_query($con, $detailsData) or die(mysqli_error($con));
//Displayed data
<?php while($detailsHuis = mysqli_fetch_assoc($detailsRes)): ?>
<div class="item overzicht-item">
<figure class="overzichtfoto">
<img src="<?php echo $detailsHuis['afbeelding']?>" alt="" class="img-responsive">
<?php if($detailsHuis['type'] == "Te koop"):?>
<span class="label koop"><?php echo $detailsHuis['type']?></span>
<?php elseif($detailsHuis['type'] == "Te huur"):?>
<span class="label huur"><?php echo $detailsHuis['type']?></span>
<?php endif;?>
<a href="huis.php?id=<?php echo $detailsHuis['id']?>"><div class="overlay-overzicht"></div></a>
</figure>
<div class="overzicht-details">
<div class="item-header clearfix">
<h3 class="overzicht-titel"><a href="huis.php?id=<?php echo $detailsHuis['id']?>">Huis in <?php echo $detailsHuis['stad']?></a></h3>
<span class="favorite"><i class="fa fa-heart" style="margin-right: 0px;"></i></span>
<span class="overzicht-plaats"><i class="fa fa-map-marker"></i><b><?php echo $detailsHuis['stad']?></b> <?php echo $detailsHuis['provincie']?></span>
</div>
<div class="item-detail overzicht-detail">
<span class="price price-overzicht">€ <?php echo $detailsHuis['prijs']?></span>
<div class="left">
<span class="bed"><?php echo $detailsHuis['kamers']?></span>
<span class="bath"><?php echo $detailsHuis['badkamers']?></span>
<span class="garage"><?php echo $detailsHuis['garage']?></span>
</div>
<div class="right">
<span class="area"><?php echo $detailsHuis['oppervlakte']?> m2</span>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
//Me trying to create a PHP Function
function insert(){
if(isset($_RETURN['value'])){
if($_RETURN['filtermenu'] == 'Amsterdam')
{
$query = "SELECT * FROM huis WHERE stad='Amsterdam'";
}
elseif($_RETURN['filtermenu'] == 'London')
{
$query = "SELECT * FROM huis WHERE stad='London'";
}
else
{
$query = "SELECT * FROM huis";
}
$sql = mysql_query($overzichtRes);
}
}
我不確定我是否正確理解你的問題,顯示數據的代碼位在哪裏?你究竟想要做什麼?你的查詢在哪裏? –
我在第一篇文章中添加了更多代碼。 –
http://www.w3schools.com/php/php_ajax_database.asp我認爲這是你正在尋找的 – SML