用yii 1实现后台的搜索功能,效果如下图:
1.模型中:
1publicfunctionsearch()2{34$criteria=newCDbCriteria;5//独立高级搜索6if(isset($_GET['goods']) ) {7//商品货号8if(isset($_GET['goods']['goods_sn']) &&$_GET['goods']['goods_sn'] != "")9{10$criteria->compare('goods_sn',$_GET['goods']['goods_sn'],true);11}12//商品名称13if(isset($_GET['goods']['goods_name']) &&$_GET['goods']['goods_name'] != "")14{15$criteria->compare('goods_name',$_GET['goods']['goods_name'],true);16}17//商品分类18if(isset($_GET['goods']['cat_id']) &&$_GET['goods']['cat_id'] != "")19{20$criteria->compare('cat_id',$_GET['goods']['cat_id'],true);21}22//是否上架23if(isset($_GET['goods']['is_on_sale']) &&$_GET['goods']['is_on_sale'] != "")24{25$criteria->compare('is_on_sale',$_GET['goods']['is_on_sale']);26}27//是否精品28if(isset($_GET['goods']['is_best']) &&$_GET['goods']['is_best'] != "")29{30$criteria->compare('is_best',$_GET['goods']['is_best']);31}32//是否新品33if(isset($_GET['goods']['is_new']) &&$_GET['goods']['is_new'] != "")34{35$criteria->compare('is_new',$_GET['goods']['is_new']);36}37//是否热销38if(isset($_GET['goods']['is_hot']) &&$_GET['goods']['is_hot'] != "")39{40$criteria->compare('is_hot',$_GET['goods']['is_hot']);41}4243}44returnnewCActiveDataPRovider($this,array(45'criteria'=>$criteria46));47}
2.控制器中:
$model=newB2cGoods('search');
表示在model中启用模型中的search作为搜索。
3.视图中:
<divclass="well">
<divclass="search-box">
<formclass="form-inline" method="get" action="">
//指定form表单提交的页面,很重要<input type='hidden' name='r' value='B2CShop/b2cGoods/goodsList/id/<?phpecho $id ?>'>
<divclass="form-group">
<input
name="goods[goods_sn]"type="text"class="form-control"style="width:140px;"placeholder= "商品货号"value=<?phpecho$_GET['goods']['goods_sn'] ; ?>
>
</div> <divclass="form-group">
<input
name="goods[goods_name]"type="text"class="form-control"style="width:140px;"placeholder= "商品名称"value=<?phpecho$_GET['goods']['goods_name'] ; ?>
>
</div> <divclass="form-group">
<?phpechoCHtml::dropDownList( "goods[cat_id]" ,$_GET['goods']['cat_id'] ,B2cCategory::listData($id) ,array( "class"=>"form-control" , 'empty'=>'请选择类型...', 'encode' =>false, "style"=>"width:140px") ); ?>
</div> <divclass="checkbox">
<label style="font-size: 16px">上架<input
type="checkbox"name="goods[is_on_sale]"style="width: 24px;"value="1"
//实现checkbox,刷新页面保持原状态<?phpecho$_GET['goods']['is_on_sale']?'checked="checked"':'' ?>
>
</label>
</div> <divclass="checkbox">
<label style="font-size: 16px">精品<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">新品<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">热销<input
type="checkbox"name="goods[is_hot]"style="width: 24px;"value="1"
<?phpecho$_GET['goods']['is_hot']?'checked="checked"':'' ?>
>
</label>
</div>
<button type="submit"class="btn btn-default"><spanclass="glyphicon glyphicon-search"></span> 搜 索</button>
</form>
</div>
</div>
这里需要注意的一点是实现checkbox,保持原状态,<?phpecho$_GET['goods']['is_hot']?'checked="checked"':'' ?>,即用php判断是否有值。