<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Target_model extends CI_Model {
protected $tbl;
protected $limit = 0;
protected $offset = 10;
function __construct() {
parent::__construct();
$this->tbl = "target";
}
function get_all($where = array(), $fields = array('*'), $limit = array(), $order_by = array()) {
$data = array();
//select field
if (is_array($fields)) {
$this->db->select(implode(',', $fields));
}
if (count($where) > 0) {
foreach ($where as $field => $value) {
switch ($field) {
default:
$this->db->where($field, $value);
break;
case "or_where":
foreach ($value as $fi => $val) {
$this->db->or_where($fi, $val);
}
break;
case "like":
foreach ($value as $fi => $val) {
$this->db->like($fi, $val);
}
break;
case "or_like":
foreach ($value as $fi => $val) {
$this->db->or_like($fi, $val);
}
break;
case "join":
foreach ($value as $fi => $val) {
$this->db->join($fi, $val);
}
break;
}
}
}
///order
if (count($order_by) > 0) {
foreach ($order_by as $field => $typ) {
$this->db->order_by($field, $typ);
}
}
//limit
if (count($limit) > 0) {
$query = $this->db->get($this->tbl, $limit['limit'], $limit['offset']);
} else {
$query = $this->db->get($this->tbl);
}
if ($result = $query->result()) {
$data['results'] = $result;
}
$data['num'] = $query->num_rows();
return $data;
}
function get_row($where = array(), $fields = array('*'), $order_by = array()) {
$data = array();
//select field
if (is_array($fields)) {
$this->db->select(implode(',', $fields));
}
if (count($where) > 0) {
foreach ($where as $field => $value) {
switch ($field) {
default:
$this->db->where($field, $value);
break;
case "or_where":
foreach ($value as $fi => $val) {
$this->db->or_where($fi, $val);
}
break;
case "like":
foreach ($value as $fi => $val) {
$this->db->like($fi, $val);
}
break;
case "or_like":
foreach ($value as $fi => $val) {
$this->db->or_like($fi, $val);
}
break;
case "join":
foreach ($value as $fi => $val) {
$this->db->join($fi, $val);
}
break;
}
}
}
//order
if (count($order_by) > 0) {
foreach ($order_by as $field => $typ) {
$this->db->order_by($field, $typ);
}
}
$query = $this->db->get($this->tbl);
if ($result = $query->row()) {
$data = $result;
}
return $data;
}
function get_row_array($where = array(), $fields = array('*'), $order_by = array()) {
$data = array();
//select field
if (is_array($fields)) {
$this->db->select(implode(',', $fields));
}
if (count($where) > 0) {
foreach ($where as $field => $value) {
switch ($field) {
default:
$this->db->where($field, $value);
break;
case "or_where":
foreach ($value as $fi => $val) {
$this->db->or_where($fi, $val);
}
break;
case "like":
foreach ($value as $fi => $val) {
$this->db->like($fi, $val);
}
break;
case "or_like":
foreach ($value as $fi => $val) {
$this->db->or_like($fi, $val);
}
break;
case "join":
foreach ($value as $fi => $val) {
$this->db->join($fi, $val);
}
break;
}
}
}
//order
if (count($order_by) > 0) {
foreach ($order_by as $field => $typ) {
$this->db->order_by($field, $typ);
}
}
$query = $this->db->get($this->tbl);
if ($result = $query->row_array()) {
$data = $result;
}
return $data;
}
function get_count($where = array(), $limit = array()) {
$data = array();
//select field
if (count($where) > 0) {
foreach ($where as $field => $value) {
switch ($field) {
default:
$this->db->where($field, $value);
break;
case "or_where":
foreach ($value as $fi => $val) {
$this->db->or_where($fi, $val);
}
break;
case "like":
foreach ($value as $fi => $val) {
$this->db->like($fi, $val);
}
break;
case "or_like":
foreach ($value as $fi => $val) {
$this->db->or_like($fi, $val);
}
break;
case "join":
foreach ($value as $fi => $val) {
$this->db->join($fi, $val);
}
break;
}
}
}
return $this->db->count_all_results($this->tbl);
}
function save($where = array(), $data = array()) {
$user_data=$this->session->userdata('logged_in');
if (count($where) > 0) {
foreach ($where as $field => $value) {
$this->db->where($field, $value);
}
$data['user_modified'] = $user_data['id'];
$this->db->update($this->tbl, $data);
return TRUE;
} else {
$data['add_date'] = date("Y-m-d H:i:s");
$data['user_add'] = $user_data['id'];
$this->db->insert($this->tbl, $data);
return $this->db->insert_id();
}
}
function delete($where = array()) {
$result = false;
if (is_array($where) && count($where) > 0) {
foreach ($where as $field => $value) {
$this->db->where($field, $value);
}
if ($this->db->delete($this->tbl)) {
$result = true;
}
}
return $result;
}
function get_sum($where = array(), $fields) {
if (count($where) > 0) {
foreach ($where as $field => $value) {
switch ($field) {
default:
$query = $this->db->where($field, $value);
break;
case "or_where":
foreach ($value as $fi => $val) {
$query = $this->db->or_where($fi, $val);
}
break;
case "like":
foreach ($value as $fi => $val) {
$query = $this->db->like($fi, $val, 'both');
}
break;
case "or_like":
foreach ($value as $fi => $val) {
$query = $this->db->or_like($fi, $val, 'both');
}
break;
case "join":
foreach ($value as $fi => $val) {
$query = $this->db->join($fi, $val);
}
break;
}
}
}
$query = $this->db->select_sum($fields, 'num');
$query = $this->db->get($this->tbl);
$result = $query->result();
return $result[0]->num;
}
function get_min($where = array(), $fields) {
if (count($where) > 0) {
foreach ($where as $field => $value) {
switch ($field) {
default:
$query = $this->db->where($field, $value);
break;
case "or_where":
foreach ($value as $fi => $val) {
$query = $this->db->or_where($fi, $val);
}
break;
case "like":
foreach ($value as $fi => $val) {
$query = $this->db->like($fi, $val, 'both');
}
break;
case "or_like":
foreach ($value as $fi => $val) {
$query = $this->db->or_like($fi, $val, 'both');
}
break;
case "join":
foreach ($value as $fi => $val) {
$query = $this->db->join($fi, $val);
}
break;
}
}
}
$query = $this->db->select_min($fields, 'num');
$query = $this->db->get($this->tbl);
$result = $query->result();
return $result[0]->num;
}
function get_max($where = array(), $fields) {
if (count($where) > 0) {
foreach ($where as $field => $value) {
switch ($field) {
default:
$query = $this->db->where($field, $value);
break;
case "or_where":
foreach ($value as $fi => $val) {
$query = $this->db->or_where($fi, $val);
}
break;
case "like":
foreach ($value as $fi => $val) {
$query = $this->db->like($fi, $val, 'both');
}
break;
case "or_like":
foreach ($value as $fi => $val) {
$query = $this->db->or_like($fi, $val, 'both');
}
break;
case "join":
foreach ($value as $fi => $val) {
$query = $this->db->join($fi, $val);
}
break;
}
}
}
$query = $this->db->select_max($fields, 'num');
$query = $this->db->get($this->tbl);
$result = $query->result();
return $result[0]->num;
}
function get_field(){
$field=$this->db->list_fields($this->tbl);
return $field;
}
}
?>
0 comments:
Post a Comment