【问题标题】:yii, search for a value from textfield in databaseyii,从数据库中的文本字段中搜索一个值
【发布时间】:2015-01-18 00:21:25
【问题描述】:
I have this code on my view,

<div class="text-id-box">
<?php echo CHtml::textField($model,'id'); ?>
</div>

<div class="submit-box">
<?php echo CHtml::button('Search', array(
'submit' => array('SiteController/actionSearch',
array('id'=>$id)))); ?>
</div>

和我的数据库: id: varchar(8) 名称:varchar(20) 位置:varchar(50)

我想在我的数据库中搜索 id,然后返回名称和位置值。如何构建它的模型和控制器。 任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 在yii中做脚手架,它会自动和grid一起创建搜索功能。

标签: php html database search yii


【解决方案1】:
  1. 首先使用 gii ref (http://www.yiiframework.com/doc/guide/1.1/en/topics.gii) 生成模型
  2. 然后您可以在控制器中添加以下功能
public function actionSearch($id)
{
 $model= new Yourmodel;
 $model=Yourmodel::model()->findByPk($id);
  //you will get the row for particular id
 // and you can access it by $model->name $model->location
 // render the required page and access to model object to get the name and location.

}    

【讨论】:

    猜你喜欢
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    相关资源
    最近更新 更多