用yii 1实现后台的搜索功能,效果如下图:
1.模型中:
public function search()
{ $criteria = new CDbCriteria;
//独立高级搜索
if(isset( $_GET['goods']) ) {
//商品货号
if (isset($_GET['goods']['goods_sn']) && $_GET['goods']['goods_sn'] != "")
{
$criteria->compare('goods_sn',$_GET['goods']['goods_sn'], true );
}
//商品名称
if (isset($_GET['goods']['goods_name']) && $_GET['goods']['goods_name'] != "")
{
$criteria->compare('goods_name',$_GET['goods']['goods_name'], true);
}
//商品分类
if (isset($_GET['goods']['cat_id']) && $_GET['goods']['cat_id'] != "")
{
$criteria->compare('cat_id',$_GET['goods']['cat_id'], true);
}
//是否上架
if (isset($_GET['goods']['is_on_sale']) && $_GET['goods']['is_on_sale'] != "")
{
$criteria->compare('is_on_sale',$_GET['goods']['is_on_sale']);
}
//是否精品
if (isset($_GET['goods']['is_best']) && $_GET['goods']['is_best'] != "")
{
$criteria->compare('is_best',$_GET['goods']['is_best']);
}
//是否新品
if (isset($_GET['goods']['is_new']) && $_GET['goods']['is_new'] != "")
{
$criteria->compare('is_new',$_GET['goods']['is_new']);
}
//是否热销
if (isset($_GET['goods']['is_hot']) && $_GET['goods']['is_hot'] != "")
{
$criteria->compare('is_hot',$_GET['goods']['is_hot']);
} }
return new CActiveDataProvider($this, array(
'criteria'=>$criteria
));
}
2.控制器中:
$model=new B2cGoods('search');
表示在model中启用模型中的search作为搜索。
3.视图中:
<div class="well">
<div class="search-box">
<form class="form-inline" method="get" action="">
//指定form表单提交的页面,很重要
<input type='hidden' name='r' value='B2CShop/b2cGoods/goodsList/id/<?php echo $id ?>'>
<div class="form-group">
<input
name="goods[goods_sn]"
type="text"
class="form-control"
style="width:140px;"
placeholder = "商品货号"
value=<?php echo $_GET['goods']['goods_sn'] ; ?>
>
</div> <div class="form-group">
<input
name="goods[goods_name]"
type="text"
class="form-control"
style="width:140px;"
placeholder = "商品名称"
value=<?php echo $_GET['goods']['goods_name'] ; ?>
>
</div>
<div class="form-group">
<?php echo CHtml::dropDownList( "goods[cat_id]" ,
$_GET['goods']['cat_id'] ,
B2cCategory::listData( $id ) ,
array( "class"=>"form-control" , 'empty'=>'请选择类型...', 'encode' => false, "style"=>"width:140px") ); ?>
</div>
<div class="checkbox">
<label style="font-size: 16px">上架
<input
type="checkbox"
name="goods[is_on_sale]"
style="width: 24px;"
value="1"
//实现checkbox,刷新页面保持原状态
<?php echo $_GET['goods']['is_on_sale']?'checked="checked"':'' ?>
>
</label>
</div>
<div class="checkbox">
<label style="font-size: 16px">精品
<input
type="checkbox"
name="goods[is_best]"
style="width: 24px;"
value="1"
<?php echo $_GET['goods']['is_best']?'checked="checked"':'' ?>
>
</label>
</div>
<div class="checkbox">
<label style="font-size: 16px">新品
<input
type="checkbox"
name="goods[is_new]"
style="width: 24px;"
value="1"
<?php echo $_GET['goods']['is_new']?'checked="checked"':'' ?>
>
</label>
</div>
<div class="checkbox">
<label style="font-size: 16px">热销
<input
type="checkbox"
name="goods[is_hot]"
style="width: 24px;"
value="1"
<?php echo $_GET['goods']['is_hot']?'checked="checked"':'' ?>
>
</label>
</div>
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span> 搜 索</button>
</form>
</div>
</div>
这里需要注意的一点是实现checkbox,保持原状态,<?php echo $_GET['goods']['is_hot']?'checked="checked"':'' ?>,即用php判断是否有值。