get_list_by_where

 /**
     * 查询数据
     * @param $param
     * @param bool $get_rows 或者总数
     * @param bool $get_one 或者一条记录
     * @param bool $master 是否主表查询
     */
    public function get_list_by_where(array $params, $get_rows = false, $get_one = false,$master=false)
    {
        $this->db->from($this->table);
        if (isset($params['select'])) {
            if (isset($params['select_escape'])) {
                $this->db->select($params['select'], false);
            } else {
                $this->db->select($params['select']);
            }
        }

        if($master){
            $this->db->force_master();
        }
        if (isset($params['where']) && is_array($params['where'])) {
            $this->db->where($params['where']);
        }

        if (isset($params['where_in']) && is_array($params['where_in'])) {
            $this->db->where_in($params['where_in']['key'], $params['where_in']['value']);
        }

        if (isset($params['join'])) {
            foreach ($params['join'] as $item) {
                $this->db->join($item['table'], $item['where'], $item['type']);
            }
        }
        if (isset($params['limit'])) {
            if (is_array($params['limit']) && isset($params['limit']['page']) && isset($params['limit']['page_size'])) {
                $this->db->limit($params['limit']['page_size'],($params['limit']['page']-1)*$params['limit']['page_size']);
            } else {
                $this->db->limit($params['limit']);
            }
        }

        if (isset($params['group'])) {
            $this->db->group_by($params['group']);
        }
        if (isset($params['order'])) {
            if (is_array($params['order'])) {
                foreach ($params['order'] as $v) {
                    $this->db->order_by($v['key'], $v['value']);
                }
            } else {
                $this->db->order_by($params['order']);
            }

        }

        $result = $this->db->get();
        if (!$get_one) {
            return $get_rows ? $result->num_rows() : ($result->num_rows() > 0 ? $result->result_array() : array());
        } else {
            return $get_rows ? $result->num_rows() : ($result->num_rows() > 0 ? $result->row_array() : array());
        }
    }

  

猜你喜欢

转载自www.cnblogs.com/php-linux/p/9591514.html