0

好的,所以我是这种事情的新手,并且正在建立一个小型工作网站,其中包含员工个人资料。我正在研究一种编辑配置文件的方法,当有人进入“编辑”页面时,我需要预先选择所有已使用的值。

当我有一个基于枚举列表的下拉列表时,它工作得很好

例子

<?php

$table_name = "agents";
$column_name = "AssignedABC";

echo "<select name=\"$column_name\"><option>Select one</option>";
$q = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'";
$r = mysqli_query($con, $q);

$row = mysqli_fetch_array($r);

$enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));
foreach($enumList as $value)
    echo '<option value="'.$value.'" '.(($value==$rows[AssignedABC])?'selected="selected"':"").'>'.$value.'</option>';

echo "</select>";
?>

当有一个多选框时会出现问题。我无法让它从数据库中预先选择所有/任何现有选择。

问题代码

<?php

$table_name = "agents";
$column_name = "Designations";

echo "<select multiple name='Designations[]'>";
$q = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'";
$r = mysqli_query($con, $q);

$row = mysqli_fetch_array($r);

$enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));
foreach($enumList as $value)

    echo '<option value="'.$value.'" '.(($value==$rows[Designations])?'selected="selected"':"").'>'.$value.'</option>';

echo "</select>";
?>

任何形式的帮助/指导将不胜感激:)

编辑/更新

<?php

$table_name = "agents";
$column_name = "Designations";
$csvString = $rows['Designations'];
$des = explode(",", $csvString);

echo "<select multiple name='Designations[]'>";
$q = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'";
$r = mysqli_query($con, $q);

$row = mysqli_fetch_array($r);

$enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));
foreach ($des as $opt)
foreach($enumList as $value)

    echo '<option value="'.$value.'" '.(($value==$opt)?'selected="selected"':"").'>'.$value.'</option>';

echo "</select>";

?>

现在一切都已正确预选.. 现在列表中的每个选项都有重复。0_o

工作代码

<?php

$table_name = "agents";
$column_name = "Designations";
$csvString = $rows['Designations'];
$des = explode(",", $csvString);

echo "<select multiple name='Designations[]'>";
$q = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'";
$r = mysqli_query($con, $q);

$row = mysqli_fetch_array($r);

$enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

foreach($enumList as $value)

    echo '<option value="'.$value.'" '.((in_array($value, $des))?'selected="selected"':"").'>'.$value.'</option>';

echo "</select>";

?>
4

0 回答 0