<?php 
   Class Data_access_model extends CI_Model {
	
        Public function __construct() { 
         parent::__construct();
         $this->load->database();
       	} 
		
		public function select_all($table_name,$order_by='',$limit=''){
			if($order_by != ''){
				 $this->db->order_by($order_by, "desc");
			}
			
			if($limit!= ''){
				$this->db->limit($limit);
				
			}
			$query =$this->db->get($table_name);
			
			return $query;
		}
		
	    public function select_data($colums,$table_name,$where,$limit='',$order_by=''){
		
		 $this->db->select($colums)->from($table_name)->where($where);
		 
		 if($order_by != ''){
				 $this->db->order_by($order_by, "desc");
			}
		 
		 if($limit != ''){
			 $this->db->limit($limit[0],$limit[1]);
		 }
		 
		 $query = $this->db->get();
		 return $query;
		}
		
		public function select_data_withlimit($colums,$table_name,$where,$perpage,$offset){
		
		 $this->db->select($colums)->from($table_name)->where($where)->limit($perpage,$offset);
		 $query = $this->db->get();
		 return $query;
		}
		
		 public function select_data_like($colums,$table_name,$where){
		
		 $this->db->select($colums)->from($table_name)->like($where);
		 $query = $this->db->get();
		 
		return $query;
		}
		
		public function insert_data($table_name,$data){
			
		  $query = $this->db->insert($table_name,$data);
		  return  $query;
		}
		
		public function update_data($table_name,$where,$data){
			
			$query = $this->db->set($data)->where($where)->update($table_name);
			return $query;
		}
	
	
		public function delete_data($table_name,$where){
			
			$query = $this->db->where($where)->delete($table_name);
			return $query;
		}
		
	   public function count_items_withlimit($data,$countitem,$as,$table,$jointable,$jointabledata,$perpage,$offset,$group_by,$order_by='')
       { 
			$query = $this->db->select(''.$data.' ,COUNT('.$countitem.') as '.$as.'')
						  ->from($table)
						  ->join($jointable, $jointabledata, 'right')
						  ->limit($perpage,$offset)
						  ->group_by($group_by)
						  ->get();
					return $query;
        } 
			
        public function count_items($data,$countitem,$as,$table,$jointable,$jointabledata,$group_by)
       { 
			$query = $this->db->select(''.$data.' ,COUNT('.$countitem.') as '.$as.'')
						  ->from($table)
						  ->join($jointable, $jointabledata, 'right')
						  ->group_by($group_by)
						  ->get();
						  
				return $query;
        }    
		
        public function count_items_where($data,$countitem,$as,$table,$where,$jointable,$jointabledata,$group_by)
       { 
			$query = $this->db->select(''.$data.' ,COUNT('.$countitem.') as '.$as.'')
						  ->from($table)
						  ->where($where)
						  ->join($jointable, $jointabledata, 'right')
						  ->group_by($group_by)
						  ->get();
						  
				return $query;
        }

		public function select_random_record($data,$tablename,$orderby,$limit){

				$random_record = $this->db->select($data)
                           ->from($tablename)
						   ->order_by($orderby, 'RANDOM')
						   ->limit($limit)
                           ->get();				
				return $random_record;
		}		
			
		public function select_random_record_where($data,$tablename,$where_data,$orderby,$limit){
					$random_record = $this->db->select($data)
                           ->from($tablename)
						   ->where($where_data)
						   ->order_by($orderby, 'RANDOM')
						   ->limit($limit)
                           ->get();				

			    return $random_record;
		}	
		
		public function get_sum_of_column($column,$tablename,$data){

				$get_sum = $this->db->select_sum($column)
                           ->from($tablename)
						   ->where($data)
						   ->get();				

			    return $get_sum;
		}	
			
	}	
?>