<?php
	
	class Controller{
		
		private $conn;
	
		
		public function __construct($conn){
			$this->conn= $conn;
		}
		
		//insert 
		public function insert($table_name, array $array){
			
			$field = implode(", ",array_keys($array));
			$escaped_values = array_map(mysql_real_escape_string, array_values($array));
			$values  = implode("','", $escaped_values);
			
			$q_insert="INSERT INTO ".$table_name." ($field) VALUES ('$values') ";    
			//echo $q_insert; exit;
			$run=mysql_query($q_insert, $this->conn);
		}
		

		//update
		public function update($table_name,array $array, $field_id, $get_id ){
			
			$q_update="update ".$table_name." set"; 
			$count_array=count($array); 
			$i=1;
			foreach($array as $field1 => $value1){
							
				if($i===$count_array){
					$q_update.=" $field1 = '$value1' "; 
				}else{
					$q_update.=" $field1 = '$value1', "; 				
				}
				$i++;
			}
		
		$q_update.="where $field_id='$get_id' ";
			//echo $q_update;  exit;
			$run=mysql_query($q_update, $this->conn);
		
		}
		

		public function getvalue($table_name,$field_id,$get_id){
					
			 $q_edit="select * from ".$table_name." where $field_id='$get_id' "; 
			 $result=mysql_query($q_edit, $this->conn) or die(mysql_error());
			return $result;
		}	
		
		//delete
		public function delete($table_name,$field_id,$delete_parmams){
					
			$q_delete="DELETE FROM ".$table_name." WHERE $field_id IN ($delete_parmams)";
			$result=mysql_query($q_delete, $this->conn) or die(mysql_error());

		}
		
		//status
		public function status($table_name, $field, $value,$field_id,$get_id){
		
			switch ($value) {
				case Y:
					$value="N";
					break;
					
				case N:
					$value="Y";
					break;
			
				}
			
			$q_status="update ".$table_name." set $field='$value' where $field_id='$get_id' "; 
			$result=mysql_query($q_status, $this->conn) or die(mysql_error());

		}	
		
		//Select
		public function getAll($table_name,$field_id){
					
			$q_getall="SELECT * FROM ".$table_name." ORDER BY ".$field_id." DESC";
			$result=mysql_query($q_getall, $this->conn) or die(mysql_error());
			return $result;
		}
		
		public function comboFill($id, $name, $table_name){

			echo $q_comboFill="SELECT ".$id.", ".$name." FROM ".$table_name." "; exit;
			$result_comboFill=mysql_query($q_comboFill);
			
			while( $obj_row = mysql_fetch_array($recresult) )
			{	
					$str_option	.=	"<option value='".$obj_row[$id]."' ";
									if($selected_value==$obj_row[$name]) {
										$str_option	.=	" selected";
									}
					$str_option	.=	">".stripslashes($obj_row[$name])."</option>";				
			}
			return $str_option;

		}

		//Login
		public function login($table_name,array $array){
					
			$q_login="SELECT * FROM ".$table_name." where";
			$count_array=count($array); 
			$i=1;
			foreach($array as $field1 => $value1){
							
				if($i===$count_array){
					$q_login.=" $field1 = '$value1' "; 
				}else{
					$q_login.=" $field1 = '$value1' and "; 				
				}
				$i++;
			}
			
			//echo $q_login; exit;
			
			$result=mysql_query($q_login, $this->conn) or die(mysql_error());
			return $result;
		}	

		// Redirect
		public function redirect($path){
			header('Location:'.$path.'.php ');
		}
		
		public function date_dmy($date){
			$date_dmy=date('d-m-Y',strtotime($date));
			return $date_dmy;
		}
		
		public function date_ymd($date){
			$date_ymd=date('Y-m-d',strtotime($date));
			return $date_ymd;
		}

		function welcome(){
 			date_default_timezone_set("Asia/Calcutta");
		   if(date("H") < 12){
		 
		     return "good morning";
		 
		   }elseif(date("H") > 11 && date("H") < 18){
		 
		     return "good afternoon";
		 
		   }elseif(date("H") > 17){
		 
		     return "good evening";
		 
		   }
		 
		}  

		public function getCount($para_select, $para_table, $para_condition){
			$sql= " SELECT $para_select FROM $para_table WHERE $para_condition ";  
			$run= mysql_query($sql);
			return $run;
		}
		
		

	}
	
	
		
?>