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