如何在具有所选值的for循环中显示多个下拉列表

时间:2014-04-30 10:51:22

标签: php html yii

我在for循环中显示了多个下拉列表,但是我无法显示其选中的值,我想在下拉列表中显示所选的帖子值,下面是我的代码 -

<?php  
                    if ( !empty($assignee) )
                    {
                        // Counter
                        $k = 0;
                        // Loop through
                        foreach ( $assignee as $assignee )
                        {

                ?>
                            <tr>
                                <td width="140"><?php echo $assignee->firstname." ".$assignee->lastname ?></td>
                                <td width="200">
                                    <?php
                                            echo CHTML::activeDropDownList(
                                                $model,
                                                'role[]',
                                                CHtml::listData(Role::model()->findAllByAttributes(array('type'=>'project')), 'id', 'name'),
                                                array('prompt' => 'Select role', 'id'=>'role_'.$k.'', 'onChange'=>'javascript:unableAssignee(this.id)',  
                                                        )
                                            );
                                     ?>
                                </td>
                                <td width="60">
                                    <input type="checkbox" name="assignee[]" id="assignee_<?php echo $k ?>" value="<?php echo $assignee->id ?>" disabled="disabled" 
                                    <?php if(!empty($_POST['assignee']) && $_POST['assignee'] == $assignee->id ) { echo "in";?> checked="checked"  <?php } ?>/>
                                </td>
                            </tr>
                <?php 
                        // Increment counter
                        $k++;
                        }
                    }   
                ?>          

2 个答案:

答案 0 :(得分:1)

我可以给你一个粗略的想法 使用此选项可从DB

中获取所选值
while($row_list1=mysql_fetch_assoc($resultstatus))
                    {                               
                        if($row['status']==$row_list1['license_status'])
                        {
                            echo '<option value="'.htmlspecialchars($row_list1['license_status']).'">'.htmlspecialchars($row_list1['license_status']).'</option>';                              
                        }
                        else
                        {
                            echo '<option value="'.htmlspecialchars($row_list1['license_status']).'">'.htmlspecialchars($row_list1['license_status']).'</option>';                              
                        }
                    }

答案 1 :(得分:0)

您写了小错字,将CHTML::更改为CHtml::

根据您的要求,首先从 htmlOptions 中删除'prompt' => 'Select role',然后在options属性中使用'selected'=>true,如下所示。

    $valueYouWant2Select='admin'; //for example
    echo CHtml::activeDropDownList(
            $model,
            'role[]',
            CHtml::listData(Role::model()->findAllByAttributes(array('type'=>'project')), 'id', 'name'),
            array(
                'options' => array($valueYouWant2Select=>array('selected'=>true)), 
                'id'=>'role_'.$k.'', 
                'onChange'=>'javascript:unableAssignee(this.id)')
            );