简体   繁体   English

如果在数据库中找到,则 PHP 过滤器 html 选择选项值

[英]PHP Filter html select option value if it is found in the database

I have a database where plots (spots/places) are saved (this can be 5, 40 ,91 etc. (not incremented)) On my website I have a dropdown option where available plots are selected (0 - 1023).我有一个保存图(点/地点)的数据库(这可以是 5、40、91 等(不增加))在我的网站上,我有一个下拉选项,可以选择可用的图(0 - 1023)。 I want the option that is found in the database to be hidden from the dropdown selection on the website.我希望从网站上的下拉选择中隐藏在数据库中找到的选项。 So if plot 1 is taken it is found in the database and it shouldn't be available in the website dropdown selection.因此,如果采用图 1,则可以在数据库中找到它,并且它不应在网站下拉选择中可用。 I can't seem to get this working...我似乎无法让这个工作......

This is the code that I came up with.这是我想出的代码。

<label for="plotnumber">Plotnumber :</label></br>
<select name="input_plotnumber">

<?php for ($i = 0; $i <= 1023; $i++) : ?>

$query = ("SELECT plotnumber FROM plot1");
$result = $conn->query($query);
$arrayofplots = mysqli_fetch_all($result);

if (in_array($i, $arrayofplots)) {

}
else {
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
}

<?php endfor; ?>?>
</select>

If you modify the SQL to select all taken plots you can then decide to only output option elements whose value is not within that recordset.如果修改 SQL 以选择所有获取的图,则可以决定仅输出值不在该记录集中的option元素。 Based upon your comment that there is a column taken you might try like this:基于您的评论,有一列taken ,你可以尝试这样的:

<label>Plotnumber :

    <br />

    <?php
        $sql='select plotnumber from plot1 where taken=1';
        $res=$conn->query($sql);
        $data=$res->fetch_all(MYSQLI_ASSOC);
    ?>


    <select name="input_plotnumber">
        <option selected disabled hidden>Select Plot
        <?php
            for( $i=0; $i < 1024; $i++ ){
                if( !in_array( $i, array_column($data,'plotnumber') ) )printf('<option>%d',$i);
            }
        ?>
    </select>

</label>
  • You need to convert the two-dimensional array returned by mysqli_fetch_all into a one-dimensional array before you can use in_array .您需要将mysqli_fetch_all返回的二维数组转换为一维数组,然后才能使用in_array
  • As you have some column to indicate if the plot is taken, you need to put that in a WHERE clause.由于您有一些列来指示是否采用了绘图,因此您需要将其放在WHERE子句中。 I have used taken=1 but if you have a string like taken/not taken then modify it accordingly.我已经使用了taken=1但如果你有一个像taken/not taken take 这样的字符串taken/not taken那么相应地修改它。
  • Avoid running a query in a loop.避免在循环中运行查询。 Instead you should fetch the data at the top.相反,您应该在顶部获取数据。
  • As a rule of thumb, choose either the object oriented style $result->fetch_all() or procedural mysqi_fetch_all() , instead of mixing both.根据经验,选择面向对象样式$result->fetch_all()或程序mysqi_fetch_all() ,而不是混合两者。

Updated code:更新代码:

<?php

$query = "SELECT plotnumber FROM plot1 WHERE taken=1";
$result = $conn->query($query);
$arrayofplots = $result->fetch_all();

// convert to a 1d array
$arrayofplots = array_map(function($row){
  return $row[0];
}, $arrayofplots);

?>

<label for="plotnumber">Plotnumber :</label></br>
<select name="input_plotnumber">

<?php
for ($i = 0; $i <= 1023; $i++){
  if (!in_array($i, $arrayofplots)) {
    echo '<option value="' . $i . '">' . $i . '</option>';
  }
}
?>

</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM