具有预先选择的多选表单的枚举列表

时间:2013-08-07 15:53:22

标签: php mysql enums selected

好的,所以我对这类事情不熟悉,正在建立一个小型网站,用于为员工提供个人资料。我正在编写一种编辑配置文件的方法,当有人进入“编辑”页面时,我需要预先选择所有已使用的值。

当我有一个基于枚举列表的下拉列表时,它可以正常工作

实施例

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

?>

0 个答案:

没有答案