简体   繁体   中英

How can i display multiple dropdown list in for loop with selected value

I have displayed multiple dropdown list in for loop, but i am not able to display its selected value, The idea is i want to display selected post values in drop-down list, below is my code -

<?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++;
                        }
                    }   
                ?>          

I can give you a rough idea Use this to get the selected value from 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>';                              
                        }
                    }

You wrote small typo, change CHTML:: to CHtml::

For your requirement, first remove 'prompt' => 'Select role' from the htmlOptions and use 'selected'=>true in options property as below.

    $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)')
            );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM