<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	
	
	class Common extends CI_Model{ 
		public function __construct()
        {
			// Call the CI_Model constructor
			parent::__construct();
		}

		
		function insert($table, $data){
			$this->db->insert($table,$data);
			$num = $this->db->insert_id();
			if($num){
				return $num;
				}else{
				return FALSE;
			}
		} 


		 function  existdata($table_name,$data){  
         
          $this->db->select('*');
          $this->db->where($data);    
          $getdata  = $this->db->get($table_name);
          
          $num      = $getdata->num_rows();
          if($num>0){ 
            $arr    = $getdata->row();
            return $arr;
          } else{
              return false;
         }
     }

     function getinsertdata($table_name,$data){
			  $this->db->select('*');
              $this->db->where($data);    
          $getdata  = $this->db->get($table_name);          
          $num = $getdata->num_rows();
          if($num>0){ 
            $arr    = $getdata->result();
            return $arr;
          } else{
              return false;
         }
		}

     
		function update($table,$where,$data){
			$this->db->where($where );
			$update = $this->db->update($table,$data);		
			if($update)	{ 
				return TRUE;
				}else{ 
				return FALSE;
			}
		} 
		
		function delete($table,$where){
                    $db_debug = $this->db->db_debug; //save setting
                    $this->db->db_debug = FALSE; //disable debugging for queries
                    $this->db->where($where);
                    //$this->db->limit('1');
                    $del = $this->db->delete($table);
                     //run query
                    //check for errors, etc
                    $this->db->db_debug = $db_debug; //restore setting
                    if($del){
                        return true;
                    }else{
                        return false;
                    }
		} 
		
                function deleteall($table,$where){
                    if($where){
			$this->db->where($where);
			$del = $this->db->delete($table);
			if($del){
				return true;
				}else{
				return false;
			}
                    }
		} 
                
		function getAll($table){
			$data = $this->db->get($table);
			$get = $data->result();
                        
			$num = $data->num_rows();
                        if($num){
				return $get;
				}else{
				return false;
			}
		}
		
		function getWhere($table,$where){	
			//$this->db->order_by("id", "desc"); 
			$this->db->where($where);
			$getdata = $this->db->get($table);		
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows)
				{
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}else{ 
				return false;
			}		
		}
		
		function getWhereSingle($table,$where){	
			//$this->db->order_by("id", "desc"); 
			$this->db->where($where);
			$getdata = $this->db->get($table);		
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows)
				{
					$data = $rows;
				}
				$getdata->free_result();
				return $data;
				}else{ 
				return false;
			}		
		}	
		function getWhere_limit($table,$where){	
			$this->db->order_by("id", "desc"); 
			$this->db->limit("1");
			$this->db->where($where);
			$getdata = $this->db->get($table);		
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows)
				{
					$data = $rows;
				}
				$getdata->free_result();
				return $data;
				}else{ 
				return false;
			}		
		}
		
		function getWhere_with_limit($table,$where,$start, $end){	//$this->db->order_by("id", "desc"); 
			$this->db->limit($end,$start);
			$this->db->where($where);
			$getdata = $this->db->get($table);		
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}else{ 
				return false;
			}		
		}
		
		function getRow($table,$where){	
			$this->db->order_by("id","desc");
			$this->db->limit("1"); 
			$this->db->where($where);
			$getdata = $this->db->get($table);		
			$num = $getdata->result();
			if($num> 0){ 
				//$arr=$getdata->row();
				return $num;
				}else{ 
				return false;
			}		
		}
		
		function getWhere_with_order($table,$where,$orderby){	//$this->db->order_by("id", "desc"); 
			$this->db->order_by($orderby, "desc");
			$this->db->where($where);
			$getdata = $this->db->get($table);		
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}else{ 
				return false;
			}		
		}
		/* shrikant assending order */
		function getWhere_with_order_asc($table,$where,$orderby){	//$this->db->order_by("id", "desc"); 
			$this->db->order_by($orderby, "asc");
			$this->db->where($where);
			$getdata = $this->db->get($table);		
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}else{ 
				return false;
			}		
		}
		
		function getWhere_with_group($table,$where,$groupby){	//$this->db->order_by("id", "desc"); 
			$this->db->group_by($groupby);
			$this->db->where($where);
			$getdata = $this->db->get($table);		
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}else{ 
				return false;
			}		
		}
		
		function getWhere_with_limit_order($table,$where,$orderby,$start, $end){	//$this->db->order_by("id", "desc"); 
			$this->db->limit($end,$start);
			$this->db->order_by($orderby, "desc");
			$this->db->where($where);
			$getdata = $this->db->get($table);		
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}else{ 
				return false;
			}		
		}
		
		function getWhere_with_some($table,$where,$select){	//$this->db->order_by("id", "desc"); 
			$this->db->select($select);
			$this->db->from($table);	
			$this->db->where($where);
			$getdata = $this->db->get();		
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}else{ 
				return false;
			}	
		}
		
		function join_three_type_with_wh($tbl1,$tbl2 ,$tbl3 ,$field1,$field2,$field3,$field4,$select,$where,$join_type){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->where($where);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->join($tbl3, $tbl1.'.'.$field3.'='.$tbl3.'.'.$field4,$join_type);
			//$this->db->order_by($tbl1.'.id','DESC');
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		
		function join_three_type_with_wh2($tbl1,$tbl2 ,$tbl3 ,$field1,$field2,$field3,$field4,$select,$where,$join_type){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->where($where);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->join($tbl3, $tbl2.'.'.$field3.'='.$tbl3.'.'.$field4,$join_type);
			//$this->db->order_by($tbl1.'.id','DESC');
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}		
		
		function find_and_set($table,$col_name,$val){ 
			$this->db->where("FIND_IN_SET('".$val."',".$col_name.") !=", 0);
			$getdata = $this->db->get($table);
			$num = $getdata->num_rows();
			if($num> 0) { 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		
		function count_row($table,$where){
			$this->db->where($where);
			$data = $this->db->get($table);	
			$num = $data->num_rows();		
			if($num){
				return $num;
				}else{
				return false;
			}
		}
		
		function getLoginDetails($table_name,$user_name,$password){  
			$this->db->select('*');
			$this->db->from($table_name);
			$this->db->where('email',$user_name);
			$this->db->where('password',$password);	
			$this->db->or_where('username',$user_name);	
			$this->db->where('password',$password);
			$getdata  = $this->db->get();
			$num 		= $getdata->num_rows();
			if($num> 0){ 
				$arr 	= $getdata->row();
				return $arr;
				} else{
				return false;
			}
		}
		
		function join_two($tbl1,$tbl2 ,$field1,$field2,$where,$select){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2);
			$this->db->where($where);		
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0) { 
				$arr=$getdata->result();
				//~ foreach ($arr as $rows){
				//~ $data = $rows;
				//~ }
				//~ $getdata->free_result();
				return $arr;
				} else{ 
				return false;
			}
		}	
		
		function join_two_orderby($tbl1,$tbl2 ,$field1,$field2,$where,$select,$orderby,$groupby,$limit=0,$offset=0){  
			$this->db->group_by($groupby); 
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2);
			$this->db->order_by($orderby, "desc");
			$this->db->where($where);	
			$this->db->limit($limit,$offset);	
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		function join_two_groupy($tbl1,$tbl2 ,$field1,$field2,$where,$select,$groupby){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2);
			$this->db->group_by($groupby); 
			$this->db->where($where);		
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		function join_two_limit($tbl1,$tbl2 ,$field1,$field2,$where,$select,$orderby,$start,$end){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2);
		    $this->db->order_by($orderby, "DESC");
			$this->db->limit($end,$start);
			$this->db->where($where);		
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		function join_two_group_by_limit($tbl1,$tbl2 ,$field1,$field2,$where,$select,$groupby,$start,$end){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2);
			$this->db->group_by($groupby); 
			$this->db->limit($end,$start);
			$this->db->where($where);		
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}  else{ 
				return false;
			}
		}
		function join_two_group_by_orderby_limit($tbl1,$tbl2 ,$field1,$field2,$where,$select,$groupby,$orderby,$start,$end){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2);
			$this->db->group_by($groupby); 
			$this->db->order_by($orderby, "DESC"); 
			$this->db->limit($end,$start);
			$this->db->where($where);		
			$getdata  = $this->db->get();
			
			//echo $this->db->last_query(); die;
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}  else{ 
				return false;
			}
		}
		function join_two_type_without_wh($tbl1,$tbl2 ,$field1,$field2,$select,$join_type){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$getdata  = $this->db->get();
			
			// echo $this->db->last_query(); die;
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}  else{ 
				return false;
			}
		}	
		
		function join_three_type_with_where($tbl1,$tbl2 ,$tbl3 ,$field1,$field2,$field3,$field4,$where,$select,$join_type){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->join($tbl3, $tbl1.'.'.$field3.'='.$tbl3.'.'.$field4,$join_type);
			$this->db->where($where);
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}  else{ 
				return false;
			}
		}
		
		function join_three_type_with_where_limit($tbl1,$tbl2 ,$tbl3 ,$field1,$field2,$field3,$field4,$where,$select,$join_type,$orderby,$start,$end){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->join($tbl3, $tbl1.'.'.$field3.'='.$tbl3.'.'.$field4,$join_type);
			$this->db->where($where);
			$this->db->order_by($orderby, "ASC"); 
			$this->db->limit($end,$start);
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}  else{ 
				return false;
			}
		}		
		
		
		function join_three_type_without_wh($tbl1,$tbl2 ,$tbl3 ,$field1,$field2,$field3,$field4,$select,$join_type){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->join($tbl3, $tbl1.'.'.$field3.'='.$tbl3.'.'.$field4,$join_type);
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}  else{ 
				return false;
			}
		}	
		
		function join_two_type($tbl1,$tbl2 ,$field1,$field2,$where,$select,$join_type){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->where($where);		
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		
		
		
		function join_two_type_orderby($tbl1,$tbl2 ,$field1,$field2,$where,$select,$join_type,$orderby){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->order_by($orderby, "DESC");  
			$this->db->where($where);		
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		function join_two_type_group_by($tbl1,$tbl2 ,$field1,$field2,$where,$select,$join_type,$goupby){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->group_by($goupby); 
			$this->db->where($where);				  
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		
		function join_two_type_limit($tbl1,$tbl2 ,$field1,$field2,$where,$select,$join_type,$orderby,$start,$end){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->where($where);		
			$this->db->order_by($orderby, "DESC"); 
			$this->db->limit($end,$start);
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		
		/********* shrikant *************/
		
		function join_two_type_limit_asc($tbl1,$tbl2 ,$field1,$field2,$where,$select,$join_type,$orderby,$start,$end){   
			$this->db->select($select);
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
			$this->db->where($where);		
			$this->db->order_by($orderby, "ASC"); 
			$this->db->limit($end,$start);
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		
		function join_two_type_limit_asc2($tbl1,$tbl2 ,$field1,$field2,$where,$select,$join_type,$orderby,$start,$end,$select1=NULL,$search=NULL,$id=NULL){
			if($search !='')
			{
				if($select1 == 'accept'){
					$status = 'accept';
				}
				else
				{
					$status = 'waiting';
				}
				$getdata = $this->db->query("SELECT `booking`.*, `users_info`.`firstname`, `users_info`.`lastname`, `users_info`.`user_id`, (select firstname from users_info where `users_info`.`user_id`= `booking`.`created_by`) as `bookedby` FROM `booking` LEFT JOIN `users_info` ON `booking`.`user_id`=`users_info`.`user_id` WHERE `booking`.`coach_id` = '".$id."' AND `booking`.`action` = '".$status."' AND `booking`.`status` = 'done' AND (`booking`.`id` LIKE '%".$search."%' OR  `users_info`.`firstname` LIKE '%".$search."%' OR  `users_info`.`lastname` LIKE '%".$search."%' OR (select firstname from users_info where `users_info`.`user_id`= `booking`.`created_by`) LIKE '%".$search."%') ORDER BY `booking`.`id` ASC LIMIT ".$start.",".$end);
			}
			else
			{
				$this->db->select($select);
				$this->db->from($tbl1);
				$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
				$this->db->where($where);		
				$this->db->order_by($orderby, "ASC"); 
				$this->db->limit($end,$start);
				$getdata  = $this->db->get();	
			}   
			//echo $this->db->last_query(); die;
		  	$num = $getdata->num_rows();
		  	if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
		   		return false;
			}
		}
		
		
		function join_two_type2($tbl1,$tbl2 ,$field1,$field2,$where,$select,$join_type,$select1=NULL,$search=NULL,$id=NULL){   
			
			if($search !='')
			{
				if($select1 == 'accept'){
					$status = 'accept';
				}
				else
				{
					$status = 'waiting';
				}
				$getdata = $this->db->query("SELECT `booking`.*, `users_info`.`firstname`, `users_info`.`lastname`, `users_info`.`user_id`, (select firstname from users_info where `users_info`.`user_id`= `booking`.`created_by`) as `bookedby` FROM `booking` LEFT JOIN `users_info` ON `booking`.`user_id`=`users_info`.`user_id` WHERE `booking`.`coach_id` = '".$id."' AND `booking`.`action` = '".$status."' AND `booking`.`status` = 'done' AND (`booking`.`id` LIKE '%".$search."%' OR  `users_info`.`firstname` LIKE '%".$search."%' OR  `users_info`.`lastname` LIKE '%".$search."%' OR (select firstname from users_info where `users_info`.`user_id`= `booking`.`created_by`) LIKE '%".$search."%') ORDER BY `booking`.`id` ASC");
			}
			else
			{
				$this->db->select($select);
				$this->db->from($tbl1);
				$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2,$join_type);
				$this->db->where($where);
				$getdata  = $this->db->get();
			}		  
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}	
		
		/*specific category function start*/
		
		function countJTAndW($tbl1,$tbl2 ,$field1,$field2,$where,$count_id){   
			$this->db->select('COUNT('.$tbl1.'.'.$count_id.') as total');
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2);
			$this->db->where($where);		  
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}  else{ 
				return false;
			}
		}	
		
		function getCustom($str_query){				
			$getdata = $this->db->query($str_query);			
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}		
		}
		
		
		
		function getLimited($table,$from ,$to){ 
			$getdata = $this->db->get($table, $from, $to);
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				} else{ 
				return false;
			}
		}
		
		function countAllRequests($tbl1,$tbl2 ,$field1,$field2,$where){
			$this->db->select('COUNT('.$tbl1.'.rs_id'.') AS total_requests'); 
			$this->db->from($tbl1);
			$this->db->join($tbl2, $tbl1.'.'.$field1.'='.$tbl2.'.'.$field2);
			$this->db->where($where);
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				foreach ($arr as $rows){
					$data[] = $rows;
				}
				$getdata->free_result();
				return $data;
				}	else{ 
				return false;
			}
		}
		function gmail_login($name,$email,$verified_email,$link,$picture,$gender,$locale){   
			$this->db->select('*');
			$this->db->from('customer');
			$this->db->where('email_id',$email);		
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0){ 
				$arr=$getdata->result();
				$session = array(
				'user_id'=>$arr[0]->id, 
				'user_name'=>$arr[0]->user_name				   
				);
				//set session for login user
				
				$this->session->set_userdata($session);
				$users=explode(" ",$arr[0]->user_name);				
				if(strpos($arr->user_name," ")){ 					
					$_SESSION['username']=str_replace(" ","_",$arr[0]->user_name)."_".$arr[0]->id;
					} else{
					$_SESSION['username']=$arr[0]->user_name."_  _".$arr[0]->id;
				}
				return 'already';
				}  else{ 
				$ips=$_SERVER['REMOTE_ADDR'];
				$datas=array(
				'user_name'=>$name,
				'email_id'=>$email,
				'email_verify'=>$verified_email,
				'gmail_link'=>$link,
				'gmail_picture'=>$picture,
				'gender'=>$gender,
				'register_date'=>date("d M Y H:m:s A"),
				'last_login'=>date("d M Y H:m:s A"),
				'login_status'=>'1',
				'signup_mode'=>'gmail',
				'ip_address'=>$ips,
				'status'=>'0'
				);
				$this->db->insert('customer',$datas);
				$last_id = $this->db->insert_id();
				$this->db->select('*');
				$this->db->from('customer');
				$this->db->where('id',$last_id);		
				$getdata  = $this->db->get();
				$arr=$getdata->result();
				$session = array('user_id'=>$arr[0]->id, 
				'user_name'=>$arr[0]->user_name				   
				);
				//set session for login user				
				$this->session->set_userdata($session);
				$users=explode(" ",$arr[0]->user_name);				
				if(strpos($arr->user_name," ")){ 					
					$_SESSION['username']=str_replace(" ","_",$arr[0]->user_name)."_".$arr[0]->id;
					} else{
					$_SESSION['username']=$arr[0]->user_name."_..._".$arr[0]->id;
				}
				return 'success';	   
			}
		}
		
		function fb_login($name=" ",$email=" ",$id=" "){   
			$this->db->select('*');
			$this->db->from('customer');
			$this->db->where('fb_ids',$id);		
			$getdata  = $this->db->get();
			$num = $getdata->num_rows();
			if($num> 0) { 
				$arr=$getdata->result();
				$session = array(
				'user_id'=>$arr[0]->id, 
				'user_name'=>$arr[0]->user_name				   
				);
				//set session for login user
				
				$this->session->set_userdata($session);
				$users=explode(" ",$arr[0]->user_name);				
				if(strpos($arr->user_name," ")){ 					
					$_SESSION['username']=str_replace(" ","_",$arr[0]->user_name)."_".$arr[0]->id;
					} else{
					$_SESSION['username']=$arr[0]->user_name."_..._".$arr[0]->id;
				}
				return 'already';
				
				}  else{ 
				$ips=$_SERVER['REMOTE_ADDR'];
				$datas=array(
				'user_name'=>$name,
				'email_id'=>$email,
				'email_verify'=>'1',
				'fb_ids'=>$id,
				'register_date'=>date("d M Y H:m:s A"),
				'last_login'=>date("d M Y H:m:s A"),
				'login_status'=>'1',
				'signup_mode'=>'fb',
				'ip_address'=>$ips,
				'status'=>'0'
				);
				$this->db->insert('customer',$datas);
				$last_id = $this->db->insert_id();
				$this->db->select('*');
				$this->db->from('customer');
				$this->db->where('id',$last_id);		
				$getdata  = $this->db->get();
				$arr=$getdata->result();
				$session = array(
				'user_id'   =>$arr[0]->id, 
				'user_name' =>$arr[0]->user_name				   
				);
				//set session for login user				
				$this->session->set_userdata($session);
				$users=explode(" ",$arr[0]->user_name);				
				if(strpos($arr->user_name," ")){ 					
					$_SESSION['username']=str_replace(" ","_",$arr[0]->user_name)."_".$arr[0]->id;
					} else{
					$_SESSION['username']=$arr[0]->user_name."_  _".$arr[0]->id;
				}
				return 'success';		   
			}
		}
		
		function mailSend($email,$type,$name,$subject,$userName=0,$password=0,$resetLink=0){ 
			$otherMessage ='';
			switch($type){
				case 'companyRegister':			  	
				$messageInit  ='Hi <span style="font-weight: bold;text-transform: capitalize;">'.$name.' , <br> <br>  Your account is created by admin <br/> Username: <b>'.$userName.' </b>, Password:<b>'.$password.'</b> <br><br> and please  activate your account click here a blow link: <a href="'.$resetLink.'" style="color: #FFF;
				font-weight: bold;background-color: #0cce0c; padding: 5px; text-decoration: none; border-radius: 5px;" target="blank">Click here </a>';
				$otherMessage ='<tr>
				<td class="panel" style="background-color: #999; width: 100%; height: 40px; border: 2px solid #222; padding-left: 30px;">
				<a href="javascript:;" style="color: #c70d0d; font-weight: bold;">'.$resetLink.'</a>
				</td>
				</tr>
				<tr>
				<td>
				<p class="m-t-15 last">If clicking the URL above does not work, copy and paste the URL into a browser window.</p>
				</td>
				</tr>';
				break;
				case 'companyRegistrationFromFront': 	  
				
				$messageInit  ='Hi <span style="font-weight: bold;text-transform: capitalize;">'.$name.' , <br> <br>   Please activate your account 
				click here a blow link: <a href="'.$resetLink.'" style="color: #FFF;
				font-weight: bold;background-color: #0cce0c; padding: 5px; text-decoration: none; border-radius: 5px;" target="blank">Click here </a>';
				$otherMessage ='<tr>
				<td class="panel" style="background-color: #999; width: 100%; height: 40px; border: 2px solid #222; padding-left: 30px;">
				<a href="javascript:;" style="color: #c70d0d; font-weight: bold;">'.$resetLink.'</a>
				</td>
				</tr>
				<tr>
				<td>
				<p class="m-t-15 last">If clicking the URL above does not work, copy and paste the URL into a browser window.</p>
				</td>
				</tr>';
				break;
				case 'wokerRegistrationFromAdmin':
				$messageInit  ='Hi <span style="font-weight: bold;text-transform: capitalize;">'.$name.' , <br> <br>  Your account is created by admin <br/> Username: <b>'.$userName.' </b>, Password:<b>'.$password.'</b> <br><br> and please  activate your account click here a blow link: <a href="'.$resetLink.'" style="color: #FFF;
				font-weight: bold;background-color: #0cce0c; padding: 5px; text-decoration: none; border-radius: 5px;" target="blank">Click here </a>';
				$otherMessage ='<tr>
				<td class="panel" style="background-color: #999; width: 100%; height: 40px; border: 2px solid #222; padding-left: 30px;">
				<a href="javascript:;" style="color: #c70d0d; font-weight: bold;">'.$resetLink.'</a>
				</td>
				</tr>
				<tr>
				<td>
				<p class="m-t-15 last">If clicking the URL above does not work, copy and paste the URL into a browser window.</p>
				</td>
				</tr>';
				
				
				break;
				case 'wokerRegistrationFromFront': 	  
				
				$messageInit  ='Hi <span style="font-weight: bold;text-transform: capitalize;">'.$name.' , <br> <br>   Please activate your account 
				click here a blow link: <a href="'.$resetLink.'" style="color: #FFF;
				font-weight: bold;background-color: #0cce0c; padding: 5px; text-decoration: none; border-radius: 5px;" target="blank">Click here </a>';
				$otherMessage ='<tr>
				<td class="panel" style="background-color: #999; width: 100%; height: 40px; border: 2px solid #222; padding-left: 30px;">
				<a href="javascript:;" style="color: #c70d0d; font-weight: bold;">'.$resetLink.'</a>
				</td>
				</tr>
				<tr>
				<td>
				<p class="m-t-15 last">If clicking the URL above does not work, copy and paste the URL into a browser window.</p>
				</td>
				</tr>';
				break;
				case 'resetPassword':
				$messageInit  ='Hi <span style="font-weight: bold;text-transform: capitalize;">'.$name.' , <br> <br>   Please click the following url or link and  reset your password : <a href="'.$resetLink.'" style="color: #FFF;
				font-weight: bold;background-color: #0cce0c; padding: 5px; text-decoration: none; border-radius: 5px;" target="blank">Click here</a>';
				$otherMessage ='<tr>
				<td class="panel" style="background-color: #999; width: 100%; height: 40px; border: 2px solid #222; padding-left: 30px;">
				<a href="javascript:;" style="color: #c70d0d; font-weight: bold;">'.$resetLink.'</a>
				</td>
				</tr>
				<tr>
				<td>
				<p class="m-t-15 last">If clicking the URL above does not work, copy and paste the URL into a browser window.</p>
				</td>
				</tr>';
				break;		
			}
			
			
			
			$config = array (
			'mailtype' => 'html',
			'charset'  => 'utf-8',
			'priority' => '1'
			);
			
		    $message = $this->mailbody->messageBody($subject,$messageInit,$otherMessage);
	        $this->email->initialize($config);
			$this->email->from(DEFAULT_EMAIL,SITE_NAME);		
			$this->email->to($email);			
			$this->email->subject($subject);
			$this->email->message($message);					
			if($this->email->send()){
				return 'Mail Send';
				}else{
				return 'Mail Failed';
			}
		}
		
		
		/******* shrikant **********/
		
		function customquery($sql){
			$data = $this->db->query($sql);
			if($data->num_rows() > 0){
				return $data->result();
			}
			return false;
		}
		/*****Library******/ 
		
		public function library_posts($search)
		{
			if($search==''){ 
				$country = trim($this->session->userdata('country_of_origin'));
				$query = $this->db->query("SELECT l.*,u.profile_pic,u.role,CONCAT_WS(' ', u.firstname, u.lastname) as username FROM users_info u JOIN library l ON u.user_id = l.from where l.country='{$country}' ORDER BY sent ASC");
				//echo $this->db->last_query(); die;
			return $query->result();}
			else{
				$country = trim($this->session->userdata('country_of_origin'));
				$query = $this->db->query("SELECT l.*,u.profile_pic,u.role,CONCAT_WS(' ', u.firstname, u.lastname) as username FROM users_info u JOIN library l ON u.user_id = l.from WHERE (l.country='{$country}') and (l.message LIKE '%{$search}%' or CONCAT_WS(' ', u.firstname, u.lastname) LIKE '%{$search}%') ORDER BY sent ASC");
				//echo $this->db->last_query(); die;
				return $query->result();
			}
		}
		
		
		function customquery1($sql){
			$data = $this->db->query($sql);
			
			if($data->num_rows() > 0){
			$get = $data->result();	
				foreach($get as $dh)
				{
				return $dh->id;
				}
			}
			return false;
		}
	}
