zoukankan      html  css  js  c++  java
  • Thinkphp列表搜索排序-----查

    一、控制器

    // 列表
        public function lst()
        {
            $model = D('Goods');
            // 获取带翻页的数据
            $data = $model->search();
            $this->assign(array(
                'data' => $data['data'],
                'page' => $data['page'],
            ));
    
            $this->display();
        }

    二、模型中数据的查询

    public function search()
        {
            /************ 搜索 ****************/
            $where = array();
            // 商品名称的搜索
            $goodsName = I('get.goods_name');
            if($goodsName)
                $where['goods_name'] = array('like', "%$goodsName%");
            // 价格的搜索
            $startPrice = I('get.start_price');
            $endPrice = I('get.end_price');
            if($startPrice && $endPrice)
                $where['price'] = array('between', array($startPrice, $endPrice));
            elseif ($startPrice)
                $where['price'] = array('egt', $startPrice);
            elseif ($endPrice)
                $where['price'] = array('elt', $endPrice);
            // 上架的搜索
            $isOnSale = I('get.is_on_sale', -1);
            if($isOnSale != -1)
                $where['is_on_sale'] = array('eq', $isOnSale); 
            // 是否删除的搜索
            $isDelete = I('get.is_delete', -1);
            if($isDelete != -1)
                $where['is_delete'] = array('eq', $isDelete); 
            /***************** 排序 ******************/
            $orderby = 'id';  // 默认排序字段
            $orderway = 'asc'; // 默认排序方式
            $odby = I('get.odby');
            if($odby && in_array($odby, array('id_asc','id_desc','price_asc','price_desc')))
            {
                if($odby == 'id_desc')
                    $orderway = 'desc';
                elseif ($odby == 'price_asc')
                    $orderby = 'price';
                elseif ($odby == 'price_desc')
                {
                    $orderby = 'price';
                    $orderway = 'desc';
                }
            }
            /************ 翻页 *************/
            // 总的记录数
            $count = $this->where($where)->count();
            // 生成翻页对象
            $page = new ThinkPage($count, 2);
            // 获取翻页字符串
            $pageString = $page->show();
            // 取出当前页的数据
            $data = $this->where($where)->limit($page->firstRow.','.$page->listRows)->order("$orderby $orderway")->select();
            
            //echo $this->getLastSql();
            
            return array(
                'page' => $pageString,
                'data' => $data,
            );
        }
  • 相关阅读:
    combobox获取dataset部分数据的方法
    vs2008设置Jquery自动感知方法
    iOS7 初体验
    数据采集[即与 WEB 相关的功能函数]
    javascript 时间倒计时效果
    有进度条圆周率计算
    python科学计算库
    python随笔
    jieba库
    数据分析模块pandas
  • 原文地址:https://www.cnblogs.com/yexiangwang/p/4928838.html
Copyright © 2011-2022 走看看