通过选择选项菜单HTML - PHP过滤SQL数据

时间:2016-06-09 10:38:12

标签: php html sql

我正在尝试在选择选项菜单上创建一个过滤器选项,但我似乎无法使其工作。 我的过滤器的目标是过滤“stad”上的数据。

表(Huis):

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">&euro; <?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);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以将onchange attr附加到select标记本身。如果您通过javascript处理此事件,则甚至不需要form标记(正如我从您的代码中看到的那样)