douzong3599 2014-09-09 10:22
浏览 151
已采纳

如何在更新函数中获取Yii dropDownList的选定值?

I have a problem in updating my model values, one of the model attributes is a number that is selected from a dropDownList, here is my code:

<?php $images = Homepage::model()->findAll();
                        if(!empty($images)){
                            $data = array();
                            $x = 1;
                            foreach ($images as $i){

                                array_push($data, $x);
                                $x++;
                            }
                ?>
                <?php echo $form->labelEx($model,'order'); ?>
                <?php echo $form->dropDownList($model, 'order', $data, array(
                    'empty'=>'Select image order',
                    'id'=>'order')); ?>

this dropDownList is containing the number of records of model (table) in the db, the problem is in the update function, for example: on the creation of (image 1) I selected 1 as the order value, then when I go to the update, I got the pre-selected option is 2 (which is the last value of the listData) instead of 1, so what is the error here ?

  • 写回答

3条回答 默认 最新

  • dtzjvj3915 2014-09-09 14:32
    关注

    Im not a Yii expert but i think you are doing it wrong, Im using this code below and it works fine im getting the id from the database and showing it in the dropDown list, try it maybe it helps or check http://www.yiiframework.com/wiki/48/by-example-chtml/#hh5 ,

        <?php echo $form->labelEx($model,'order_id'); ?>
        <?php echo $form->dropDownList($model,'order_id',
         CHtml::listData(Homepage::model()->findAll(),'order_id','order_id'),
         array('empty'=>'--Select order --') ); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系