<?php namespace App\Modules\Coa\Models;

class Mdl_coa
{

    protected $db;
    public function connect() 
    {
	//	$this->db =  \Config\Database::connect();
		
	}
    
    	/*
	**	Insert Query 
	*/
	function insert($table, $data) {
		$this->db->insert($table, $data);
		$id = $this->db->insert_id();
		return $id;
	}
	
	/*
	**	Update Query 
	*/
	function update($table, $col, $val, $data) {
		$this->db->where($col, $val);
		return $this->db->update($table, $data);
	}
	
	/*
	**	Delete By Id Query 
	*/
	function deleteById($table, $col, $val) {
		$this->db->where($col, $val);
		$this->db->delete($table);
	}
	
	/*
	**	Get Record By Value Query 
	*/
	function getByValue($table, $col, $val){
	    
	    $db = \Config\Database::connect();
	    $mysql_query = 'SELECT * FROM '.$table.' WHERE '.$col.' = "'.$val.'"';
		$query = $db->query($mysql_query);
		return $query->getResult(); 
			
		/*$this->db->where($col, $val);
		$query=$this->db->get($table);
		if($query->num_rows() > 0){
			return $query->result(); 
		}else{
			return "No Data";
		} */
	}

	/*
	**	Get Record By Values Query 
	*/
	/*function getByValues($table, $param) {
        $db = \Config\Database::connect();
        
        $db=table($table);
//        
		foreach($param as $key => $value){
			$db=where($key, $value);
		}
		$query=get();
    	return $query->getResult(); 
	}*/
	
	
	function getByValues1($table, $param) 
	{
        $db = \Config\Database::connect();
         $mysql_query = 'SELECT * FROM '.$table.' WHERE '.$param.' ';
		$query = $db->query($mysql_query);
		$data= $query->getResult(); 
		//print_r($data);
		return $data;
      
	}

	/*
	**	Get All Records Query 
	*/
	function getAllRecords($table) {
	    
	    $db = \Config\Database::connect();
	    $mysql_query = 'SELECT * FROM '.$table;
		$query = $db->query($mysql_query);
		return $query->getResult();
		
		/*$query=$this->db->get($table);
		if($query->num_rows() > 0){
			return $query->result(); 
		}else{
			return "No Data";
		} */
	}
	
	/*
	**	isExist Query
	*/
	function isExist($table, $col, $val){
		$this->db_->where($col, $val);
		$query=$this->db_->get($table);
		if($query->num_rows() > 0){
			return TRUE; 
		}else{
			return FALSE;
		} 
	}
	
	/*
	**	Active Query
	*/
	function active($table, $col, $val){
		$this->db->where($col, $val);
		return $this->db->update($table, array('isActive'=>'1'));
	}
	
	/*
	**	Inactive Query
	*/
	function inactive($table, $col, $val){
		$this->db->where($col, $val);
		return $this->db->update($table, array('isActive'=>'0'));
	}
	
	/*
	**	Custom Query
	*/
	function customQuery($mysql_query) {
	   $db = \Config\Database::connect();
		$query = $db->query($mysql_query);
		//if($query->num_rows() > 0){
			return $query->getResult(); 
    //}else{
    //  return "No Data";
    //}
	}

  function count($table){
    return $this->db->count_all_results($table);
	}
}