<?php 
	class connection{
		public $servername = "localhost";
		public $username = "root";
		public $password = "";
		public $dbname;
		public $conn;
		
		function __construct($dbname)
		{
			$this->conn = new mysqli($this->servername, $this->username, $this->password, $dbname);
			if ($this->conn->connect_error) {
				die("Connection failed: " . mysql_error());
			} 
		}
		
		
		function file_upload($targetDir, $fileUpld, $upldSize)
		{
			$target_dir = $targetDir;
			
			/***new code*****/
			$target_file1 = basename($_FILES["fileToUpload"]["name"]);
			$exp=explode(".", $target_file1);
			$exp1=$exp[0];
			$exp2=$exp[1];
			$newexp=$_GET['prop'];
			$filename=str_replace($exp1,$newexp, $target_file1);
			$target_file = $target_dir . $filename;
			/***new code*****/
			
			//$target_file = $target_dir . basename($_FILES[$fileUpld]["name"]);
			$uploadOk = 1;
			
			$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
			// Check if image file is a actual image or fake image
			//if(isset($_POST["submit"])) {
				$check = getimagesize($_FILES[$fileUpld]["tmp_name"]);
				if($check !== false) {
					
					$uploadOk = 1;
				} else {
					echo "<script>alert('File is not an image.')</script>";
					$uploadOk = 0;
				}
			//}
			// Check if file already exists
			if (file_exists($target_file)) {
				echo "<script>alert('Sorry, file already exists.')</script>";
				$uploadOk = 0;
			}
			// Check file size
			if ($_FILES[$fileUpld]["size"] > $upldSize) {
				echo "<script>alert('Sorry, your file is too large.')</script>";
				$uploadOk = 0;
			}
			// Allow certain file formats
			if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
			&& $imageFileType != "gif") {
				echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.')</script>";
				$uploadOk = 0;
			}
			// Check if $uploadOk is set to 0 by an error
			if ($uploadOk == 0) {
				echo "<script>alert('Sorry, your file was not uploaded.')</script>";
			// if everything is ok, try to upload file
			} else {
				if (move_uploaded_file($_FILES[$fileUpld]["tmp_name"], $target_file)) {
					echo "<script>alert('The file ". basename( $_FILES[$fileUpld]["name"]). " has been uploaded.')</script>";
					
				} else {
					echo "<script>alert('Sorry, there was an error uploading your file.')</script>";
				}
			}
			
			return $uploadOk;
		}
		
		function viewdata($sqlview){
			$sql = $sqlview;
			return $this->conn->query($sql);
					
		}
		
		function countdata($sqlview){
			$sql = $sqlview;
			$res = $this->conn->query($sql);
			return $res->num_rows;
					
		}
		
		function insertion($sqlinsert){
			$sql = $sqlinsert;//"INSERT INTO Tablename (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')";
			
			if ($this->conn->query($sql) === TRUE) {
				$msg = true;
			} else {
				$msg = false;
			}
			return $msg;
		}
		function insertion_last_id_view($sqlinsert){
			$sql = $sqlinsert;//"INSERT INTO Tablename (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')";
			
			$exe = $this->conn->query($sql);
			$lastid = $this->conn->insert_id;
			
			return $lastid;
		}
		function updation($sqlupdate){
			$sql = $sqlupdate;//"UPDATE Tablename SET lastname='Doe' WHERE id=2";
			
			if ($this->conn->query($sql) === TRUE) {
				$mgs = true;
			} else {
				$mgs = false;
			}
			return $mgs;
		}
		function deletion($sqldelete){
			$sql = $sqldelete;//"DELETE FROM Tablename WHERE id=3";

			if ($this->conn->query($sql) === TRUE) {
				$mgs = "Record deleted successfully";
			} else {
				$mgs = "Error deleting record: " . $this->conn->error;
			}
			return $mgs;
		}
		function close(){
			$this->conn->close();
		}
		
		function moneyFormatIndia($number)
		{
			if ($number >= 0)
				$sing = ' ';
			else
				$sing = '-';
		
			if($number==0)
				return 0;
		
			$n = strval(abs($number));
		
			$pos = strpos($n, '.');
		
			$whole_num = '';
			$fraction = '00';
			if ($pos === false) {
				$whole_num = $n;
			} else {
				$num_arr = explode('.', $n);
				$whole_num = $num_arr[0];
				$fraction = $num_arr[1];
			}
		
		
			if (strlen($fraction) < 2)
				$fraction .= '0';
		
			/*  DETERMINE THE FRACTION PART */
			if (intval($fraction) > 0) {
				$thired_dig = substr($fraction, 2, 1);
		
				if (intval($thired_dig) >= 5) {
					$fraction_dig = substr($fraction, 0, 2);
					$fraction_dig_final = $fraction_dig + 1;
				} else {
					$fraction_dig = substr($fraction, 0, 2);
					$fraction_dig_final = $fraction_dig;
				}
		
			} else {
				$fraction_dig_final = '00';
			}
		
		
			$n = strval(abs($whole_num));
			$len = strlen($n); //lenght of the no
			$thousend = substr($n, $len - 3, 3); //get the last 3 digits
		
			$n = strval(substr($n, 0, $len - 3)); //omit the last 3 digits already stored in $num
		
			$arr_nums = array();
			while ($n > 1) {
				$len = strlen($n);
				array_push($arr_nums, substr($n, $len - 2, 2));
				$n = strval(substr($n, 0, $len - 2));
			}
		
			$arr_nums = array_reverse($arr_nums);
			$whole_num_final = implode(',', $arr_nums) . ',' . $thousend;
		
			$final_result = $sing . $whole_num_final . '.' . $fraction_dig_final;
			return $final_result;
		
		}
		function ConNotoWords($amt)
		{
		   $number = $amt;
		   $no = round($number);
		   $point = round($number - $no, 2) * 100;
		   $hundred = null;
		   $digits_1 = strlen($no);
		   $i = 0;
		   $str = array();
		   $words = array('0' => '', '1' => 'one', '2' => 'two',
			'3' => 'three', '4' => 'four', '5' => 'five', '6' => 'six',
			'7' => 'seven', '8' => 'eight', '9' => 'nine',
			'10' => 'ten', '11' => 'eleven', '12' => 'twelve',
			'13' => 'thirteen', '14' => 'fourteen',
			'15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen',
			'18' => 'eighteen', '19' =>'nineteen', '20' => 'twenty',
			'30' => 'thirty', '40' => 'forty', '50' => 'fifty',
			'60' => 'sixty', '70' => 'seventy',
			'80' => 'eighty', '90' => 'ninety');
		   $digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
		   while ($i < $digits_1) {
			 $divider = ($i == 2) ? 10 : 100;
			 $number = floor($no % $divider);
			 $no = floor($no / $divider);
			 $i += ($divider == 10) ? 1 : 2;
			 if ($number) {
				$plural = (($counter = count($str)) && $number > 9) ? 's' : null;
				$hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
				$str [] = ($number < 21) ? $words[$number] .
					" " . $digits[$counter] . $plural . " " . $hundred
					:
					$words[floor($number / 10) * 10]
					. " " . $words[$number % 10] . " "
					. $digits[$counter] . $plural . " " . $hundred;
			 } else $str[] = null;
		  }
		  $str = array_reverse($str);
		  $result = implode('', $str);
		  $points = ($point) ?
			"." . $words[$point / 10] . " " . 
				  $words[$point = $point % 10] : '';
			return $result . "Rupees  " . $points . " Paise";
		}
	}
?>