首页 > 解决方案 > 如果数据库中存在选项值,则隐藏

问题描述

我正在尝试编写一个脚本来检查数据库并且如果它们已经存在于数据库中则不显示选项值但我不知道如何去做。以下是我到目前为止所拥有的。任何帮助都会被评估。我在这里查看了几个类似的主题,但它们并不真正适合我想要做的事情。我还在下面放了一个示例表,以更好地解释我在寻找什么。因此,如果一个 pseron 将标志 2 和 3 分配给他们,则只有选项 1 是可见的。

<?php
                $flag["infractions"] = [];
                $sql = "SELECT *
FROM flag_info
INNER JOIN infraction_flags 
ON flag_info.flag_id = infraction_flags.flag_id
WHERE ban_id = ?;";
                $stmt = $conn->prepare($sql);
                $stmt->bind_param("s", $banId);
                $stmt->execute();
                $result = $stmt->get_result();
                if ($result->num_rows != 0)
                {
                    echo "<table width='85%'>";
                    while ($row = $result->fetch_assoc())
                    {
                        $id = $row['flag_id'];
                        $longName = $row['flag_long_name'];
                        echo "<tr>";
                        echo "<td>";
                        echo $longName . "&nbsp";
                        echo "</td>";
                        echo "<form action='includes/deleteFlags.inc.php' method='POST'>";
                        echo "<input type='hidden' name='ban_id' value='" . $banId . "'>";
                        echo "<input type='hidden' name='flag_id' value='" . $id . "'>";
                        echo "<td>";
                        echo "<input type='submit' name='delete' value='[X]'></td>";
                    }
                }
                echo "</tr>";
                echo "</table>";
                echo "</form>";
                echo "<hr>";
                $sql3 = "SELECT * FROM flag_info";
                $stmt = $conn->prepare($sql3);
                $stmt->execute();
                $result = $stmt->get_result();
                echo "<form action='includes/assignFlags.inc.php' method='POST'>";
                echo "<select name='flag_id'>";
                while ($flg = $result->fetch_assoc())
                {   
                    if ($flg['flag_id'] = $id) {   //This is the logic to display the option values                 
                     echo "<option value='" . $flg['flag_id'] . "'>" . $flg['flag_long_name'] . "</option>";   
                }
                }
                echo "</select>";
                echo "<input type='hidden' name='ban_id' value='" . $banId . "' size='1'>";
                echo "<input type='submit' name='submit'>";
                echo "</form>";

infraction_flags 表

+--------+--------+---------+
| id     | ban_id | flag_id |
+========+========+=========+
|1       | 15     | 3       |
+--------+--------+---------+ 
|2       | 15     | 2       |
+--------+--------+---------+
|3       | 3      | 1       |
+--------+--------+---------+  
|4       | 1      | 1       |
+--------+--------+---------+ 

标签: php

解决方案


您可以先获取所有 ID 并放入一个数组中。看到$flag_ids我已经输入了你的代码。并使其内爆以使其像 1、2、3、4 等...然后现在选择您的标志并使用 WHERE NOT IN 进行过滤

选项1

<?php

$flag["infractions"] = [];
$sql = "SELECT *
FROM flag_info
INNER JOIN infraction_flags 
ON flag_info.flag_id = infraction_flags.flag_id
WHERE ban_id = ?;";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $banId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows != 0) {
    $flag_ids = array();
    echo "<table width='85%'>";
    while ($row = $result->fetch_assoc()) {
        $flag_ids[] = $row['flag_id']; //put all the flag id inside an array
        $longName = $row['flag_long_name'];
        echo "<tr>";
        echo "<td>";
        echo $longName . "&nbsp";
        echo "</td>";
        echo "<form action='includes/deleteFlags.inc.php' method='POST'>";
        echo "<input type='hidden' name='ban_id' value='" . $banId . "'>";
        echo "<input type='hidden' name='flag_id' value='" . $id . "'>";
        echo "<td>";
        echo "<input type='submit' name='delete' value='[X]'></td>";
    }
}
echo "</tr>";
echo "</table>";
echo "</form>";
echo "<hr>";
if(!empty($flag_ids)) {
    $where_in = implode(',', ); //set the array like 1, 2, 3, 4
    $sql3 = "SELECT * FROM flag_info WHERE flag_id NOT IN(" . $where_in . ")";
    $stmt = $conn->prepare($sql3);
    $stmt->execute();
    $result = $stmt->get_result();
    echo "<form action='includes/assignFlags.inc.php' method='POST'>";
    echo "<select name='flag_id'>";
    while ($flg = $result->fetch_assoc()) {
        echo "<option value='" . $flg['flag_id'] . "'>" . $flg['flag_long_name'] . "</option>";
    }
    echo "</select>";
    echo "<input type='hidden' name='ban_id' value='" . $banId . "' size='1'>";
    echo "<input type='submit' name='submit'>";
    echo "</form>";
}

选项2:如果您仍想显示选择偶数选项为空

<?php

$flag["infractions"] = [];
$sql = "SELECT *
FROM flag_info
INNER JOIN infraction_flags 
ON flag_info.flag_id = infraction_flags.flag_id
WHERE ban_id = ?;";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $banId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows != 0) {
    $flag_ids = array();
    echo "<table width='85%'>";
    while ($row = $result->fetch_assoc()) {
        $flag_ids[] = $row['flag_id']; //put all the flag id inside an array
        $longName = $row['flag_long_name'];
        echo "<tr>";
        echo "<td>";
        echo $longName . "&nbsp";
        echo "</td>";
        echo "<form action='includes/deleteFlags.inc.php' method='POST'>";
        echo "<input type='hidden' name='ban_id' value='" . $banId . "'>";
        echo "<input type='hidden' name='flag_id' value='" . $id . "'>";
        echo "<td>";
        echo "<input type='submit' name='delete' value='[X]'></td>";
    }
}
echo "</tr>";
echo "</table>";
echo "</form>";
echo "<hr>";
$where_in = implode(',', ); //set the array like 1, 2, 3, 4
$where_in = empty($where_in) ? '0' : $where_in;
$sql3 = "SELECT * FROM flag_info WHERE flag_id NOT IN(" . $where_in . ")";
$stmt = $conn->prepare($sql3);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows != 0) {
  echo "<form action='includes/assignFlags.inc.php' method='POST'>";
  echo "<select name='flag_id'>";
  while ($flg = $result->fetch_assoc()) {
      echo "<option value='" . $flg['flag_id'] . "'>" . $flg['flag_long_name'] . "</option>";
  }
}
echo "</select>";
echo "<input type='hidden' name='ban_id' value='" . $banId . "' size='1'>";
echo "<input type='submit' name='submit'>";
echo "</form>";


推荐阅读