Vid
Vid

Reputation: 97

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

Upvotes: 0

Views: 1061

Answers (2)

Hearaman
Hearaman

Reputation: 8726

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

Upvotes: 0

Professor
Professor

Reputation: 618

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>';                              
                        }
                    }

Upvotes: 1

Related Questions