|
本文实例讲述了ThinkPHP使用getlist方法实现数据搜索功能。分享给大家供大家参考,具体如下:
自己在ThinkPHP之中的model之中书写getlist方法,其实所谓的搜索功能无非就是数据库查询之中用到的like %string%,或者其他的 字段名=特定值,这些sql语句拼接在and语句之中;
HTML之中:
订单号: |
|
下单日期:
|
至
交易完成日期
至
订单金额:
至
| 采购商名称: |
| 采购商账号: |
|
|
看到没GET方法提交表单,这个是查询条件填入选项;
控制器之中:
$order_msg=$order->getList();
$this->assign('info',$order_msg);//这个获取订单的详细信息
Model之中:
public function getList($pagesize=25){
$tableName = $this->getTableName();
$where = $tableName.'.service_id = '.$_SESSION['service_site']['service_id'];
if(!empty($_GET['order_sn'])){//查询订单号
$where.= " and $tableName.`order_sn` like '%".$_GET['order_sn']."%'";
}
if(!empty($_GET['count_price_min'])){//查询订单最小金额
$where.= " and $tableName.count_price >=".$_GET['count_price_min']."";
}
if(!empty($_GET['begintime'])){//下单开始日期搜索
$_GET['begintime']=strtotime($_GET['begintime']);//将日期转为时间戳
$where.= " and $tableName.add_time >=".$_GET['begintime']."";
$_GET['begintime']=date('Y-m-d',$_GET['begintime']);//将日期转为时间戳
}
if(!empty($_GET['endtime'])){//下单结束日期搜索
$_GET['endtime']=strtotime($_GET['endtime']);//将日期转为时间戳
$where.= " and $tableName.add_time =".$_GET['finishbegintime']."";
$_GET['finishbegintime']=date('Y-m-d',$_GET['finishbegintime']);//将日期转为时间戳
}
if(!empty($_GET['finishendtime'])){//交易完成结束日期搜索
$_GET['finishendtime']=strtotime($_GET['finishendtime']);//将日期转为时间戳
$where.= " and $tableName.ok_time where($where)->count();
$this->countNum = $count;
$Page = new \Think\Page($count,$pagesize);
$this->page = $Page->show();
$limit = $Page->firstRow.','.$Page->listRows;
$sql="select $tableName.*,fab_supplier.nick_name as supplier_nick_name,fab_user.nick_name as user_nick_name
from ($tableName left join fab_supplier on fab_order_info.supplier_id=fab_supplier.supplier_id)
left join fab_user on fab_order_info.user_id=fab_user.user_id where $where order by $tableName.`order_id` desc limit $limit";
$sqls="select sum(fab_order_info.count_price) as order_price,count(fab_order_info.count_price) as order_count
from $tableName where $where order by $tableName.`order_id` desc limit $limit";
$this->sql_msg=$this->query($sqls);
return $this->query($sql);//订单详细信息
}
你只需要留意那个GET数据获取,然后进行拼接SQL语句;你为何总是拼接错误呢!!!
getTableName();
$count = $this->where($where)->count();
$Page = new \Think\Page($count,$pagesize);
$this->page = $Page->show();
$limit = $Page->firstRow.','.$Page->listRows;
return $this->query("select * from $tableName where $where order by $tableName.`id` asc limit $limit ");
}
public function getPage(){
return $this->page;
}
}
精简通用版getlist,实用于分页。
getTableName();
$count = $this->where($where)->count();
$Page = new \Think\Page($count,$pagesize);
$this->page = $Page->show();
$limit = $Page->firstRow.','.$Page->listRows;
return $this->query("select * from $tableName where $where order by $tableName.`id` asc limit $limit ");
}
public function getPage(){
return $this->page;
}
}
精简版MODEL用于数据自动验证
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
您可能感兴趣的文章:PHP文本数据库的搜索方法php设计模式 DAO(数据访问对象模式)PHP统计nginx访问日志中的搜索引擎抓取404链接页面路径实例讲解php数据访问php数据访问之增删改查操作php数据访问之查询关键字PHP页面输出搜索后跳转下一页的处理方法php注册审核重点解析(数据访问)php出租房数据管理及搜索页面
|
|