【问题标题】:How can i display multiple dropdown list in for loop with selected value如何在 for 循环中显示具有选定值的多个下拉列表
【发布时间】:2014-04-30 10:51:22
【问题描述】:

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

【问题讨论】:

    标签: php html yii


    【解决方案1】:

    我可以给你一个粗略的想法 使用它从数据库中获取选定的值

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

    【讨论】:

      【解决方案2】:

      你写了一个小错字,把CHTML::改成CHtml::

      根据您的要求,首先从 htmlOptions 中删除 'prompt' =&gt; 'Select role',然后在 options 属性中使用 'selected'=&gt;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)')
                  );
      

      【讨论】:

      • 感谢您的回复,但我的下拉列表在数组中,如果我使用您的代码,它将为所有下拉列表显示相同的选定值...
      猜你喜欢
      • 2020-03-06
      • 2021-04-01
      • 2014-06-06
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2023-02-08
      • 2020-07-10
      相关资源
      最近更新 更多