找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1896|回复: 0
打印 上一主题 下一主题

ThinkPHP使用getlist方法实现数据搜索功能示例

[复制链接]

2652

主题

2652

帖子

7872

积分

论坛元老

Rank: 8Rank: 8

积分
7872
跳转到指定楼层
楼主
发表于 2018-2-14 05:26:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了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出租房数据管理及搜索页面
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端