如何检查一个数组的值是否等于另一个数组的值并显示在选择下拉列表中选择的值

时间:2019-05-24 11:03:55

标签: php html mysql mysqli

我有两个数组,一个包含所有专业的列表,另一个具有所选项目的专业的列表 ,数据将从数据库中获取。我想遍历专业数组列表,并且如果它包含任何项目专业,我想将其显示为选择下拉列表中的选定项。

我曾尝试使用嵌套的while循环来遍历两个数组,但是它只显示一个专业而不是两个

$majorList = $majorObject->getICTProjectMajor(); // row[0]

$assignedMajor = $projectMajorObject->getProjectMajor(28); //row[0]
if ($assignedMajor != false) {
    while ($assignedMajorRow = mysqli_fetch_array($assignedMajor)) {
        while ($row = mysqli_fetch_array($majorList)) {


            echo "<option value='$row[0]'";
            foreach ($assignedMajorRow as $majorID) {
                if ($majorID == $row[0]) {
                    echo " selected";
                }
                echo ">$row[1]</option>'";
            }


            echo "</select>";

        }
        echo "</select>";

    }

}

功能

public function getProjectMajor($projectID)
{

    if ($this->dbc != NULL) {
        $selectQuery = "SELECT Major.majorID FROM ProjectMajor JOIN Major on Major.majorID= ProjectMajor.majorID where projectID=$projectID";
        $selectResult = mysqli_query($this->dbc, $selectQuery);
        if ($selectResult != FALSE) {
            return $selectResult;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

public function getICTProjectMajor()
{
    if ($this->dbc != NULL) {
        $selectQuery = "SELECT * FROM Major where Major.programmeID=4 ";
        $selectResult = mysqli_query($this->dbc, $selectQuery);
        if ($selectResult != FALSE) {
            return $selectResult;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

选择列表当前仅显示一个专业而不是两个

0 个答案:

没有答案