Chinaunix首页 | 论坛 | 博客
  • 博客访问: 909068
  • 博文数量: 91
  • 博客积分: 803
  • 博客等级: 准尉
  • 技术积分: 1051
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 13:42
文章分类

全部博文(91)

文章存档

2021年(1)

2020年(4)

2019年(4)

2018年(9)

2017年(11)

2016年(11)

2015年(6)

2014年(3)

2013年(28)

2012年(14)

分类: PHP

2016-06-21 10:27:44


点击(此处)折叠或打开

  1. <?php
  2. class lms_model {
  3.     public $table = 'table_name';

  4.     /**
  5.      * and 条件在前 or 在后
  6.      */
  7.     public $where_array = array(
  8.       array( 'part_sql' => ' time_day >=? ' , 'key' => 'time_day_start', 'op' => 'AND' , 'type' => 'where') ,
  9.      array( 'part_sql' => ' time_day <=? ' , 'key' => 'time_day_end', 'op' => 'AND' , 'type' => 'where') ,
  10.      array( 'part_sql' => ' group by ? ' , 'key' => 'org' , 'type' => 'group') ,
  11.      array( 'part_sql' => ' order by ? desc ' , 'key' => 'id' , 'type' => 'order') ,
  12.      array( 'part_sql' => ' limit ? ' , 'key' => 'limit' , 'type' => 'limit')
  13.     );
  14.     /**
  15.      * 拼接sql
  16.      */
  17.     public function get_where($request) {

  18.         if( empty($request) ) {
  19.             return '';
  20.         }

  21.         extract($request);

  22.         $sql = $where_sql = $group_sql = $order_sql = $limit_sql = '';

  23.         $bind_data = array();
  24.         
  25.         //拼接where

  26.         foreach($this->where_array as $k => $v) {
  27.          if( isset($$v['key']) && $v['type'] == 'where') {
  28.              $where_sql .= $v['op'] . $v['part_sql'];
  29.              $bind_data[] = $$v['key'];
  30.              continue;
  31.          }

  32.          if( isset($$v['key']) && $v['type'] == 'group') {
  33.              $group_sql .= $v['part_sql'];
  34.              $bind_data[] = $$v['key'];
  35.                 continue;
  36.          }

  37.          if( isset($$v['key']) && $v['type'] == 'order') {
  38.              $order_sql .= $v['part_sql'];
  39.              $bind_data[] = $$v['key'];
  40.                 continue;
  41.          }

  42.          if( isset($$v['key']) && $v['type'] == 'limit') {
  43.              $limit_sql .= $v['part_sql'];
  44.              $bind_data[] = $$v['key'];
  45.                 continue;
  46.          }
  47.         }


  48.         if(!empty($where_sql)) $sql = ' where ' . ltrim($where_sql,"and");

  49.         $sql .= $group_sql;

  50.         $sql .= $order_sql;

  51.         $sql .= $limit_sql;
  52.         
  53.         return array('sql' => $sql , 'bind_data' => $bind_data);
  54.     }
  55.     public function insert() {
  56.     }
  57.     public function update() {
  58.     }
  59.     public function delete() {
  60.     }
  61.     /**
  62.      * args['field'] default *
  63.      * args['where'] default null
  64.      * args['group'] default null
  65.      * args['order'] default null
  66.      * args['limit'] default null
  67.      */
  68.     public function get_data($args) {
  69.         $where = $this->get_where($args);
  70.         $field='*';
  71.         if( isset($args['field']) && !empty($args['field']) ) $field = $args['field'];
  72.         $sql = " SELECT {$field} FROM {$this->table} {$where['sql']}";
  73.         echo $sql;
  74.         echo "
    "
    ;
  75.         print_r($where['bind_data']);
  76.         echo "
    "
    ;
  77.     }
  78.     /**
  79.      * args['field'] default *
  80.      * args['where'] default null
  81.      * args['group'] default null
  82.      * args['order'] default null
  83.      */
  84.     public function get_one($args) {
  85.         $args['limit'] = 1;
  86.         $where = $this->get_where($args);
  87.         $field='*';
  88.         if( isset($args['field']) && !empty($args['field']) ) $field = $args['field'];
  89.         $sql = " SELECT {$field} FROM {$this->table} {$where['sql']}";
  90.         echo $sql;
  91.         echo "
    "
    ;
  92.         print_r($where['bind_data']);
  93.         echo "
    "
    ;
  94.     }
  95. }
  96. $model = new lms_model();
  97. $model->get_data( array('time_day_start' => '20160101','time_day_end' => 21231) );
  98. $model->get_one( array('time_day_start' => '20160101','time_day_end' => 21231) );

阅读(2067) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~