用yii 1實現後台的搜索功能,效果如下圖:
1.模型中:
1 public function search() 2 { 3 4 $criteria = new CDbCriteria; 5 //獨立高級搜索 6 if(isset( $_GET['goods']) ) { 7 //商品貨號 8 if (isset($_GET['goods']['goods_sn']) && $_GET['goods']['goods_sn'] != "") 9 { 10 $criteria->compare('goods_sn',$_GET['goods']['goods_sn'], true ); 11 } 12 //商品名稱 13 if (isset($_GET['goods']['goods_name']) && $_GET['goods']['goods_name'] != "") 14 { 15 $criteria->compare('goods_name',$_GET['goods']['goods_name'], true); 16 } 17 //商品分類 18 if (isset($_GET['goods']['cat_id']) && $_GET['goods']['cat_id'] != "") 19 { 20 $criteria->compare('cat_id',$_GET['goods']['cat_id'], true); 21 } 22 //是否上架 23 if (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 //是否精品 28 if (isset($_GET['goods']['is_best']) && $_GET['goods']['is_best'] != "") 29 { 30 $criteria->compare('is_best',$_GET['goods']['is_best']); 31 } 32 //是否新品 33 if (isset($_GET['goods']['is_new']) && $_GET['goods']['is_new'] != "") 34 { 35 $criteria->compare('is_new',$_GET['goods']['is_new']); 36 } 37 //是否熱銷 38 if (isset($_GET['goods']['is_hot']) && $_GET['goods']['is_hot'] != "") 39 { 40 $criteria->compare('is_hot',$_GET['goods']['is_hot']); 41 } 42 43 } 44 return new CActiveDataProvider($this, array( 45 'criteria'=>$criteria 46 )); 47 }
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" placeholder = "商品貨號" value=<?php echo $_GET['goods']['goods_sn'] ; ?> > </div> <div class="form-group"> <input name="goods[goods_name]" type="text" class="form-control" 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>上架 <input type="checkbox" name="goods[is_on_sale]" value="1"
//實現checkbox,刷新頁面保持原狀態 <?php echo $_GET['goods']['is_on_sale']?'checked="checked"':'' ?> > </label> </div> <div class="checkbox"> <label>精品 <input type="checkbox" name="goods[is_best]" value="1" <?php echo $_GET['goods']['is_best']?'checked="checked"':'' ?> > </label> </div> <div class="checkbox"> <label>新品 <input type="checkbox" name="goods[is_new]" value="1" <?php echo $_GET['goods']['is_new']?'checked="checked"':'' ?> > </label> </div> <div class="checkbox"> <label>熱銷 <input type="checkbox" name="goods[is_hot]" 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判斷是否有值。