php - 如果数据库中存在选项值,则隐藏
问题描述
我正在尝试编写一个脚本来检查数据库并且如果它们已经存在于数据库中则不显示选项值但我不知道如何去做。以下是我到目前为止所拥有的。任何帮助都会被评估。我在这里查看了几个类似的主题,但它们并不真正适合我想要做的事情。我还在下面放了一个示例表,以更好地解释我在寻找什么。因此,如果一个 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 . " ";
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 |
+--------+--------+---------+
解决方案
您可以先获取所有 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 . " ";
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 . " ";
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>";
推荐阅读
- tableau-api - 基于 Tableau 上的不同列进行筛选
- sql - 以智能方式在 db 中进行数据离散化
- amazon-web-services - AWS CloudFormation 堆栈的 CPU 单元
- html - 谷歌网络字体在移动 Safari 和桌面 Chrome 上的呈现方式不同?
- python - Pip list 与 conda list 显示不同的包版本
- python - 如何使用嵌套的 np.where 语句有条件地在数据框的列中复制上述案例
- python - 是什么导致这些 QLabels 周围的间距?
- asp.net - 请按名称指明首选端点配置部分
- python-3.x - TkCalendar:尝试在屏幕上同时显示多个 DateEntries 会导致 TclError
- r - 重新排列行和列 R