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