Thinkphp列表搜索排序-----查
2015-11-01 22:11
656 查看
一、控制器
二、模型中数据的查询
// 列表 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 \Think\Page($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, ); }
相关文章推荐
- leetcode-01 twoSum php实现
- PHP mysql 事务处理实例
- Thinkphp增加操作(Controller到模型Model的逻辑)
- php中对MYSQL操作之事务控制,回滚
- php mysql事务处理回滚操作
- FTP上传助手1.1 [免费版]
- PHP之闭包详解
- php面向对象(二)
- php面向对象(一)
- PHP实现QQ快速登录
- laravel中ubuntu下执行php artisan migrate总是报错
- php实战之使用递归读取商品无限分类
- vsftpd说明,有用,转了
- PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
- 重读php文档
- ThinkPHP学习实践(二)单字母函数用法
- DATASNAP中间件调用带OUTPUT参数的存储过程
- 关于php的register_globals和magic_quotes_gpc参数配置
- OS X 10.11下PHPstorm php-cgi not found 并且访问项目目录502
- phpstrom+xdebug 配置