选项值如果存在于数据库中则隐藏



我试图写一个脚本,将检查数据库,而不是显示选项值,如果它们已经存在于数据库中,但我不知道如何去做。下面是我到目前为止所做的。任何帮助都将不胜感激。我在这里看过几个类似的主题,但它们并不适合我想要做的。我还在下面放了一个示例表,以便更好地解释我在寻找什么。因此,如果一个人将标志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>";
<<p>infraction_flags表/strong>
+--------+--------+---------+
| id     | ban_id | flag_id |
+========+========+=========+
|1       | 15     | 3       |
+--------+--------+---------+ 
|2       | 15     | 2       |
+--------+--------+---------+
|3       | 3      | 1       |
+--------+--------+---------+  
|4       | 1      | 1       |
+--------+--------+---------+ 

您可以首先获取所有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:如果你仍然想显示select even选项为空

<?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>";

使用MySQL内部连接。并设置where条件flag_id = 2或flag_id = 3

最新更新