Use Yii 1 to achieve the background search function, such as:
1. In the Model:
1 Public functionSearch ()2 {3 4 $criteria=NewCdbcriteria;5 //Independent Advanced Search6 if(isset($_get[' Goods ']) ) {7 //article number8 if(isset($_get[' Goods '] [' GOODS_SN ']) &&$_get[' Goods '] [' GOODS_SN ']! = "")9 {Ten $criteria->compare (' Goods_sn ',$_get[' Goods '] [' GOODS_SN '],true ); One } A //Product Name - if(isset($_get[' Goods '] [' Goods_name ']) &&$_get[' Goods '] [' goods_name ']! = "") - { the $criteria->compare (' Goods_name ',$_get[' Goods '] [' Goods_name '],true); - } - //Product Categories - if(isset($_get[' Goods '] [' cat_id ']) &&$_get[' Goods '] [' cat_id ']! = "") + { - $criteria->compare (' cat_id ',$_get[' Goods '] [' cat_id '],true); + } A //whether shelves at if(isset($_get[' Goods '] [' Is_on_sale ']) &&$_get[' Goods '] [' Is_on_sale ']! = "") - { - $criteria->compare (' Is_on_sale ',$_get[' Goods '] [' Is_on_sale ']); - } - //whether the boutique - if(isset($_get[' Goods '] [' Is_best ']) &&$_get[' Goods '] [' is_best ']! = "") in { - $criteria->compare (' Is_best ',$_get[' Goods '] [' Is_best ']); to } + //whether new - if(isset($_get[' Goods '] [' Is_new ']) &&$_get[' Goods '] [' is_new ']! = "") the { * $criteria->compare (' Is_new ',$_get[' Goods '] [' Is_new ']); $ }Panax Notoginseng //is hot - if(isset($_get[' Goods '] [' Is_hot ']) &&$_get[' Goods '] [' is_hot ']! = "") the { + $criteria->compare (' Is_hot ',$_get[' Goods '] [' Is_hot ']); A } the + } - return NewCactivedataprovider ($this,Array( $' Criteria ' =$criteria $ )); -}
2. In the controller:
$model=new b2cgoods (' Search ');
Indicates that search in the model is enabled for searching in models.
3. In the View:
<divclass= "Well" > <divclass= "Search-box" > <formclass= "Form-inline" method= "Get" action= "" >
//Specify the page that form form submits, it is important<input type= ' hidden ' name= ' r ' value= ' b2cshop/b2cgoods/goodslist/id/<?php echo $id?> ' > <divclass= "Form-group" > <Input Name= "GOODS[GOODS_SN]"type= "Text"class= "Form-control"style= "width:140px;"placeholder= "article number"value=<?phpEcho $_get[' Goods '] [' GOODS_SN '];?> > </div> <divclass= "Form-group" > <Input Name= "Goods[goods_name]"type= "Text"class= "Form-control"style= "width:140px;"placeholder= "Product Name"value=<?phpEcho $_get[' Goods '] [' Goods_name '];?> > </div> <divclass= "Form-group" > <?phpEchoCHtml::d ropdownlist ("goods[cat_id]",$_get[' Goods '] [' cat_id '],b2ccategory:: ListData ($id) ,Array("Class" = "Form-control", ' empty ' = ' Please select Type ... ', ' encode ' =false, "style" = "width:140px")); ?> </div> <divclass= "checkbox" > <label style= "font-size:16px" >Shelves<input type= "checkbox"name= "Goods[is_on_sale]"style= "WIDTH:24PX;"value= "1"
//Implement CheckBox, Refresh page remains in original state<?phpEcho $_get[' Goods '] [' Is_on_sale ']? ' checked= "Checked" ': '?> > </label> </div> <divclass= "checkbox" > <label style= "font-size:16px" >Boutique<input type= "checkbox"name= "Goods[is_best]"style= "WIDTH:24PX;"value= "1" <?phpEcho $_get[' Goods '] [' Is_best ']? ' checked= "Checked" ': '?> > </label> </div> <divclass= "checkbox" > <label style= "font-size:16px" >New Products<input type= "checkbox"name= "Goods[is_new]"style= "WIDTH:24PX;"value= "1" <?phpEcho $_get[' Goods '] [' Is_new ']? ' checked= "Checked" ': '?> > </label> </div> <divclass= "checkbox" > <label style= "font-size:16px" >Hot<input type= "checkbox"name= "Goods[is_hot]"style= "WIDTH:24PX;"value= "1" <?phpEcho $_get[' Goods '] [' Is_hot ']? ' checked= "Checked" ': '?> > </label> </div> <b Utton type= "Submit"class= "Btn Btn-default" ><spanclass= "Glyphicon glyphicon-search" ></span> search cable </button> </form> </DIV>&L T;/div>
One thing to note here is to implement a checkbox to keep the original state, <?php echo $_get[' goods ' [' is_hot ']? ' checked= "Checked" ': '?>, that is, using PHP to determine whether there is a value.
Yii 1 Development Diary-----------search function and the implementation of checkbox