如何在编辑页面php中显示选定的多个下拉列表值

时间:2015-10-09 05:58:39

标签: php

我得到一个问题。我无法以编辑形式显示选定的dropdon值。同时添加我在数据库字段中存储dropdon值。

code:
 <select name='weekoff[]' id='weekoff' multiple>
                          <option value="Monday">Monday</option>
                          <option value="Tuesday">Tuesday</option>
                          <option value="Wednesday">Wednesday</option>
                          <option value="Thursday">Thursday</option>
                           <option value="Friday">Friday</option>
                            <option value="Saturday">Saturday</option>
                             <option value="Sunday">Sunday</option>
                        </select>

if(isset($_REQUEST['weekoff'])){
  $dbCheckbox = implode(',',$_REQUEST['weekoff']);
}

database Storing Format:Monday,Sunday

how to display selected values in edit form:
edit.php:
<?php 
                                       // $dbCheckbox = explode(',',$row['UNIT_WEEKOFF']);
                                       $weekendArr = array();
                                       $weekendArr[] =$row['UNIT_WEEKOFF'];

                    //print_r($weekendArr);
                    $weekendArr = explode(",",$weekendArr) ;
                    //echo $weekendArr;
                                      ?>
                                  <select name='weekoff[]' id='weekoff' multiple>
    <option value="Monday" <?php echo (isset($weekendArr) && in_array('Monday', $weekendArr) ) ? "selected" : "" ?>>Monday</option>
    <option value="Tuesday" <?php echo (isset($weekendArr) && in_array('Tuesday', $weekendArr) ) ? "selected" : "" ?>>Tuesday</option>
    <option value="Wednesday" <?php echo (isset($weekendArr) && in_array('Wednesday', $weekendArr) ) ? "selected" : "" ?>>Wednesday</option>
    <option value="Thursday">Thursday</option>
    <option value="Friday">Friday</option>
    <option value="Saturday">Saturday</option>
    <option value="Sunday">Sunday</option>

1 个答案:

答案 0 :(得分:0)

希望这会对你有所帮助

假设您正在从DB中检索数据,如

$weekendArr = array(); //First define blank array so you will fill it with your DB values
while($row = mysql_fetch_assoc($query)){
   $weekendArr[] = $row['weekoff']; 
}
$weekendArr = explode(",",$weekendArr) ;

然后使用此数组

<select name='weekoff[]' id='weekoff' multiple>
        <option value="Monday" <?php echo (isset($weekendArr) && in_array('Monday, $weekendArr) ) ? 'selected="selected"' : "" ?>>Monday</option>
        <option value="Tuesday" <?php echo (isset($weekendArr) && in_array('Tuesday, $weekendArr) ) ? 'selected="selected"' : "" ?>>Tuesday</option>
        <option value="Wednesday" <?php echo (isset($weekendArr) && in_array('Wednesday, $weekendArr) ) ? 'selected="selected"' : "" ?>>Wednesday</option>
        <option value="Thursday">Thursday</option>
        <option value="Friday">Friday</option>
        <option value="Saturday">Saturday</option>
        <option value="Sunday">Sunday</option>
</select>

将此选项应用于每个选项。