【问题标题】:how to fill textfield with database value in Yii?如何在 Yii 中用数据库值填充文本字段?
【发布时间】:2012-03-29 11:21:31
【问题描述】:

我想用数据库中的值填充其中一个文本字段。

通常,推荐使用此代码:

<div class="row">
  <?php echo $form->labelEx($model,'field1'); ?>
  <?php echo Model1::model()->FindByPk($model->id)->field1;?>
  <?php echo $form->error($model,'field1'); ?>
</div>

但是,我正在搜索的是将值填充到表单的文本字段中。

谁能帮帮我?谢谢..

【问题讨论】:

    标签: php yii textfield


    【解决方案1】:

    试试这个:

    <?php echo Chtml::textField('txtFieldName', Model1::model()->FindByPk($model->id)->field1); ?>
    

    【讨论】:

      【解决方案2】:

      你看过Yii's Blog Demo吗?它也可以在下载包中找到。

      通常建议这样做:

      <?php
      echo $form->labelEx($model, 'field', 'label text');
      echo $form->textField($model, 'field');
      echo $form->error($model, 'field');
      ?>
      

      如果模型包含数据库中的数据,它会在视图加载时显示出来。 您在控制器操作中的代码可能如下所示:

      // ...
      $model = Model1::model()->findByPk($id); // $id has to be the primary key of the model you want to load
      $this->render('viewfile', array(
          'model'=>$model, // this $model is then the same $model as in the view
      ));
      

      【讨论】:

      • 等一下,我关心的目的是将值填充到表单的文本字段中。不仅仅是被展示。谢谢
      • 使用此表单,您既可以显示数据库数据,也可以将用户输入存储到模型中。
      猜你喜欢
      • 2016-01-03
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多