<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class MY_model extends CI_Model
{
	protected $table_name = '';
	protected $id_name = '';
	
	function __construct()
	{
		parent::__construct();
	}
	
	function insert($data)
	{
		return $this->db->insert($this->table_name, $data);
	}
	
	function update($id, $data)
	{
		$this->db->where($this->id_name, $id);
		return $this->db->update($this->table_name, $data);
	}
	
	function delete($id)
	{
		return $this->db->delete($this->table_name, array($this->id_name => $id));
	}
	
	function search_by_id($id)
	{
		$this->db->select('*')->from($this->table_name)->where($this->id_name, $id);
		$query = $this->db->get();
		if($query->num_rows() > 0)
		{
			$row = $query->row_array();
			return $row;
		}
	}
	
	function get_list()
	{
		$query = $this->db->get($this->table_name);
		return $query->result();
	}
	
	function check_exist($data)
	{
		$sql="SELECT COUNT(".$this->id_name.") as total FROM ".$this->table_name." WHERE ".$data['field']." = '".$data['value']."' LIMIT 1";
		
		$result = $this->db->query($sql);
		$aryResult = $result->row_array();
		if (!isset($aryResult['total'])) return false;
		if ((int)$aryResult['total'] >= 1) return true; else return false;
	}
}
