• Yii 查询 搜索


    一、视图

         

    <div class="form-horizontal">
        <?php $form = $this->beginWidget('CActiveForm', array('id' => 'course', 'action' => '/admin/mp/')); ?>
        <div class="form-group">
            <div class="col-md-2">
                <?php echo $form->textField($model, 'title', array('class' => 'form-control', 'placeholder' => '标题')); ?>
            </div>
            <div class="col-md-2">
                <?php echo $form->textField($model, 'name', array('class' => 'form-control', 'placeholder' => '名字')); ?>
            </div>
            <div class="col-md-2">
                <?php echo $form->dropDownList($model, 'type', array('' => '类别') + $model->getTypeList(), array('class' => 'form-control', 'placeholder' => '类别')); ?>
            </div>
            <div class="col-md-1">
                <?php echo CHtml::submitButton('查询', array('class' => 'btn btn-default btn-primary')); ?>
            </div>
            <div class="col-md-1">
                <a href="/admin/user/edit" class="btn btn-default">添加矩阵</a>
            </div>
        </div>
        <?php $this->endWidget(); ?>
    </div>

    二、模型

    class Mp extends CActiveRecord
    {
    
        const AFFAIRS  = 1;
        const BUSINESS = 2;
    
        public function tableName()
        {
            return 'mp';
        }
    
        public static function model($className = __CLASS__)
        {
            return parent::model($className);
        }
    
        public function getTypeList()
        {
            return array(
                self::AFFAIRS => '政务',
                self::BUSINESS => '商业',
            );
        }
    
        public function getTypeName()
        {
            $arr = $this->getTypeList();
            if (isset($arr[$this->type])) {
                return $arr[$this->type];
            }
            return '未知';
        }
        public function rules()
        {
            return array(
                array('title,name,type', 'safe', 'on' => 'search'),
            );
        }
    
    }

    三、控制器

     public function actionIndex()
        {
            $model = new Mp('search');
            if (isset($_POST['Mp'])) {
                $model->setAttributes($_POST['Mp']);
            }
            $criteria = new CDbCriteria();
            $criteria->addSearchCondition('title', $model->title);
            $criteria->addSearchCondition('name', $model->name);
            $criteria->addSearchCondition('type', $model->type);
            $count = Mp::model()->count($criteria);
            $pager = new CPagination($count);
            $pager->pageSize = $this->pageSize;
            $pager->applyLimit($criteria);
    
            $list = Mp::model()->findAll($criteria);
            $this->render('index',array('model'=>$model,'list'=>$list,'pager'=>$pager));
    
        }
  • 相关阅读:
    看完一本,加油
    一个简单的动作,让你的手机号码变成空号
    Goldwave心得
    UML设计初步 基本概念一(actor, use case)
    ASP.NET控件开发 概念和HelloWorld控件
    控件的呈现
    ASP.NET控件生命周期
    ASP老项目中如何搜索一个文件在哪些地方被引用
    PL/SQL语法 游标
    2009编程语言排名
  • 原文地址:https://www.cnblogs.com/rainblack/p/5531927.html
Copyright © 2020-2023  润新知