> 백엔드 개발 > PHP 튜토리얼 > Yii 1开发日记,yii开发日记_PHP教程

Yii 1开发日记,yii开发日记_PHP教程

WBOY
풀어 주다: 2016-07-12 09:04:47
원래의
1066명이 탐색했습니다.

Yii 1开发日记,yii开发日记

用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判断是否有值。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1071503.htmlTechArticleYii 1开发日记,yii开发日记 用yii 1实现后台的搜索功能,效果如下图: 1.模型中: 1 public function search() 2 { 3 4 $criteria = new CDbCriteria; 5 // 独立...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿