First, the View
<divclass= "Form-horizontal" > <?php$form=$this->beginwidget (' Cactiveform ',Array(' id ' = ' course ', ' action ' = '/admin/mp/')); ?> <divclass= "Form-group" > <divclass= "Col-md-2" > <?phpEcho $form->textfield ($model, ' title ',Array(' class ' = ' Form-control ', ' placeholder ' = ' title ')); ?> </div> <divclass= "Col-md-2" > <?phpEcho $form->textfield ($model, ' name ',Array(' class ' = ' Form-control ', ' placeholder ' = ' + ') '); ?> </div> <divclass= "Col-md-2" > <?phpEcho $form->dropdownlist ($model, ' type ',Array(' = ' + ' category ') +$model->gettypelist (),Array(' class ' = ' Form-control ', ' placeholder ' = ' category '); ?> </div> <divclass= "Col-md-1" > <?phpEchoChtml::submitbutton (' query ',Array(' class ' = ' btn btn-default btn-primary ')); ?> </div> <divclass= "Col-md-1" > <a href= "/admin/user/edit"class= "Btn Btn-default" > Add matrix </a> </div> </div> <?php$this->endwidget ();?></div>
Second, the model
classMpextendscactiverecord{ConstAffairs = 1; ConstBusiness = 2; Public functionTableName () {return' MP '; } Public Static functionModel$className=__class__) { returnParent::model ($className); } Public functiongettypelist () {return Array( Self:: Affairs = ' Government ', Self:: Business = ' commerce ', ); } Public functionGettypename () {$arr=$this-gettypelist (); if(isset($arr[$this-type])) { return $arr[$this-type]; } returnUnknown; } Public functionrules () {return Array( Array(' Title,name,type ', ' safe ', ' on ' = ' search '), ); }}
Third, the controller
Public functionActionindex () {$model=NewMp (' Search '); if(isset($_post[' Mp '])) { $model->setattributes ($_post[' Mp ']); } $criteria=NewCdbcriteria (); $criteria->addsearchcondition (' title ',$model-title); $criteria->addsearchcondition (' name ',$model-name); $criteria->addsearchcondition (' type ',$model-type); $count= Mp::model ()Count($criteria); $pager=NewCpagination ($count); $pager->pagesize =$this-pageSize; $pager->applylimit ($criteria); $list= Mp::model ()->findall ($criteria); $this->render (' Index ',Array(' model ' =$model, ' list ' =$list, ' pager ' =$pager)); }
Yii Query Search