php - enum list with pre-selected multiselect form -
ok, new sort of thing , building small site work has profiles employees. working on way edit profiles, , when goes "edit" page, need of used values pre-selected.
when have dropdown based on enum list, works fine
example
<?php $table_name = "agents"; $column_name = "assignedabc"; echo "<select name=\"$column_name\"><option>select one</option>"; $q = "select column_type information_schema.columns table_name = '$table_name' , 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 $value) echo '<option value="'.$value.'" '.(($value==$rows[assignedabc])?'selected="selected"':"").'>'.$value.'</option>'; echo "</select>"; ?>
the problem happens when there multi-select box. cannot pre-select all/any of existing selections database.
problem code
<?php $table_name = "agents"; $column_name = "designations"; echo "<select multiple name='designations[]'>"; $q = "select column_type information_schema.columns table_name = '$table_name' , 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 $value) echo '<option value="'.$value.'" '.(($value==$rows[designations])?'selected="selected"':"").'>'.$value.'</option>'; echo "</select>"; ?>
any sort of help/guidance appreciated :)
edit/update
<?php $table_name = "agents"; $column_name = "designations"; $csvstring = $rows['designations']; $des = explode(",", $csvstring); echo "<select multiple name='designations[]'>"; $q = "select column_type information_schema.columns table_name = '$table_name' , 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 $opt) foreach($enumlist $value) echo '<option value="'.$value.'" '.(($value==$opt)?'selected="selected"':"").'>'.$value.'</option>'; echo "</select>"; ?>
everything pre-selecting now.. there repeats of every option in list now. 0_o
working code
<?php $table_name = "agents"; $column_name = "designations"; $csvstring = $rows['designations']; $des = explode(",", $csvstring); echo "<select multiple name='designations[]'>"; $q = "select column_type information_schema.columns table_name = '$table_name' , 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 $value) echo '<option value="'.$value.'" '.((in_array($value, $des))?'selected="selected"':"").'>'.$value.'</option>'; echo "</select>"; ?>
Comments
Post a Comment