如果其值已存在,如何从下拉列表中禁用该选项

时间:2015-04-22 03:56:24

标签: php mysql

我必须选择所有课程,让用户将选择的内容插入另一个名为Curriculumcourses的表格。

当用户选择课程时,有一个下拉列表可以在组中插入课程

我需要做的是,如果该小组在课程表中,该组的选项将被禁用

我的代码现在的问题是我无法从课程表中选择,因为有一个while循环来获取课程数据

这是我的代码

<?php

$curid=isset($_GET['id']) ? $_GET['id'] : '';
$sqlcheckgroup =  ("SELECT * FROM curriculumcourses  WHERE curriculum_id='$curid'");
$rowgroupcheck = mysqli_fetch_assoc($sqlcheckgroup);

$sql1 = ("SELECT * FROM courses ORDER BY course_code ");
$result = $mysqli->query($sql1);


if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {   
?>
    <tr>
      <th scope="row"> <label>

        <select name="select[]"  id="select[]">
          <option selected="selected" disabled="disabled">Please Select</option>
          <option value="0">No Group</option>
          <option value="1" <?php if (!(strcmp(1, $rowgroupcheck ['set_number']))) {echo "disabled=\"disabled\"";}?>>Group One</option>
          <option value="2" <?php if (!(strcmp(2, $rowgroupcheck ['set_number']))) {echo "disabled=\"disabled\"";}?>>Group TWO</option>
          <option value="3" <?php if (!(strcmp(3, $rowgroupcheck ['set_number']))) {echo "disabled=\"disabled\"";}?>>Group Three</option>
          <option value="4" <?php if (!(strcmp(4, $rowgroupcheck ['set_number']))) {echo "disabled=\"disabled\"";}?>>Group Four</option>
          <option value="5" <?php if (!(strcmp(5, $rowgroupcheck ['set_number']))) {echo "disabled=\"disabled\"";}?>>Group Five</option>
        </select>

1 个答案:

答案 0 :(得分:3)

将它们存储在array

$rowgroupcheck = array();
while($res = mysqli_fetch_assoc($sqlcheckgroup)) {
    $rowgroupcheck[] = $res['set_number'];
}

然后在显示选项时检查它们。 -

<option value="2" <?php if (in_array(2, $rowgroupcheck)) {echo "disabled=\"disabled\"";}?>>Group TWO</option>

执行查询 -

mysqli_query($createdlink, $yourquery)