<?php
//include("connectdb.php");

require "myclass.php";
//include "config.php";



error_reporting(E_ERROR | E_WARNING | E_PARSE);
//updations July 2019
// the function returns the assocatied array containing
	// the field name and field value pair for record.
	//
	// strTable:		table name.
	// strCriteria:		where criteria
	//
	function GetRecord($strTable, $strCriteria)
	{
		$strQuery = "select * from $strTable ";

		if(!empty($strCriteria))
			$strQuery .= "where $strCriteria;";
		
		//$nResult = MySQLQuery($strQuery);
		//return mysqli_fetch_array($nResult);
		//return mysqli_fetch_assoc($nResult);




$h = new myclass();
$DB_DBName=$h->DB_DBName;
$DB_Server=$h->DB_Server;
$DB_Username=$h->DB_Username;
$DB_Password=$h->DB_Password;



 //$connectme = new mysqli($GLOBALS["$DB_Server"], $GLOBALS["$DB_Username"], $GLOBALS["$DB_Password"],$GLOBALS["$DB_DBName"]) or die("Connect failed: %s\n". $conn -> error);
 
 $connectme = mysqli_connect($DB_Server, $DB_Username, $DB_Password,$DB_DBName) or die("Connect failed: %s\n". $conn -> error); 
//$query="Select * from tbluser where usr_name= '$username' and usr_password= '$password' ";


$result = mysqli_query($connectme, $strQuery);

return mysqli_fetch_array($result);

//return $rstUser;
	}


	function InsertRec($strTable, $arrValue)
	{
		$strQuery = "	insert into $strTable (";

		reset($arrValue);
		while(list ($strKey, $strVal) = each($arrValue))
		{
			$strQuery .= $strKey . ",";
		}

		// remove last comma
		$strQuery = substr($strQuery, 0, strlen($strQuery) - 1);

		$strQuery .= ") values (";

		reset($arrValue);
		while(list ($strKey, $strVal) = each($arrValue))
		{
			$strQuery .= "'" . FixString($strVal) . "',";
		}

		// remove last comma
		$strQuery = substr($strQuery, 0, strlen($strQuery) - 1);
		$strQuery .= ");";

$h2 = new myclass();
$DB_DBName=$h2->DB_DBName;
$DB_Server=$h2->DB_Server;
$DB_Username=$h2->DB_Username;
$DB_Password=$h2->DB_Password;

 $connectme = new mysqli($DB_Server, $DB_Username, $DB_Password,$DB_DBName) or die("Connect failed: %s\n". $conn -> error); 



$result = mysqli_query($connectme, $strQuery);

//return mysqli_fetch_array($result);

		// execute query
		//MySQLQuery($strQuery);
		//echo $strQuery . "<br>";
		
		// return id of last insert record
		//return mysqli_insert_id();
}




function CostCenterWithCode1($strName, $nSelId = -1, $blnAll=1)
        {
                $strUnique = mktime();

                $strCCStruct = getValOfTable("tblConfiguration", "conf_value", "conf_key = 'GL_CC_STRUCTURE'");
                $nLen = strlen($strCCStruct);

                $strQuery = "select * from tblCostCenter order by cc_code";
                $nResult = MySQLi_Query($connectme,$strQuery);

                if($blnAll)
                        echo "<select name='$strName'>\n\r";
                else
                        echo "<select name='$strName' onChange=\"func_check_$strUnique(this)\">\n\r";

                while($rstRow = mysqli_fetch_array($nResult))
                {
                        $nStart = 0;
                        $strCode = $rstRow["cc_code"];
                        $strName = $rstRow["cc_name"];
                        $nId = $rstRow["cc_id"];
                        
                        if(substr($strCode, 1, 2) != str_pad("", 2, "0"))
                                        $strName = "&nbsp;&nbsp;&nbsp;&nbsp;" . $strName;
                        if(substr($strCode, 4, 2) != str_pad("", 2, "0"))
                                        $strName = "&nbsp;&nbsp;&nbsp;&nbsp;" . $strName;

                        if(!$blnAll)
                        {
                                $nChk = substr($strCode, strlen($strCode)-1, strlen($strCode));
                                $nChk = doubleval($nChk);
                                if($nChk == 0)
                                        $nId = 0;
                        }

                        if($nSelId == $strCode)
                                echo "<option value='$strCode' SELECTED>".$strCode . "\n\r";
                        else
                                echo "<option value='$strCode'>".$strCode . "\n\r" ;
                }

                echo "</select>\n\r";

                if(!$blnAll)
                {
                        echo "
                        <script>
                                function func_check_$strUnique(obj)
                                {
                                        if(obj.value.substring(4, 6) == '00')
                                        {
                                                alert('Please select lowest level value');
                                                obj.value = '';
                                        }
                                }
                        </script>
                                ";
                }
        }



  function getValOfTable($strTableName, $strField, $strWhere)
        {
                if(!empty($strWhere))
                        $strQuery  = "SELECT $strField AS nCnt FROM $strTableName WHERE $strWhere";
                else
                        $strQuery  = "SELECT $strField AS nCnt FROM $strTableName";

                $nResult = MySQLi_Query($connectme,$strQuery);
                $rstRow = mysqli_fetch_array($nResult);                
                return $rstRow["nCnt"];
        }


function UpdateRec($strTable, $strWhere, $arrValue)
	{
		$strQuery = "	update $strTable set ";

		reset($arrValue);

		while (list ($strKey, $strVal) = each ($arrValue))
		{
			$strQuery .= $strKey . "='" . FixString($strVal) . "',";
		}

		// remove last comma
		$strQuery = substr($strQuery, 0, strlen($strQuery) - 1);

		$strQuery .= " where $strWhere;";

		// execute query
		//MySQLQuery($strQuery);	
$h2 = new myclass();
$DB_DBName=$h2->DB_DBName;
$DB_Server=$h2->DB_Server;
$DB_Username=$h2->DB_Username;
$DB_Password=$h2->DB_Password;

 $connectme = new mysqli($DB_Server, $DB_Username, $DB_Password,$DB_DBName) or die("Connect failed: %s\n". $conn -> error); 



$result = mysqli_query($connectme, $strQuery);	
	}



function FixString($strString)
	{
		$strString = str_replace("'", "''", $strString);
		$strString = str_replace("\'", "'", $strString);
		
		return $strString;
	}

function GetRecord2($strTable, $strCriteria)
	{
		$strQuery = "select * from $strTable ";

		if(!empty($strCriteria))
			$strQuery .= "where $strCriteria;";
		
		//$nResult = MySQLQuery($strQuery);

//$conn = mysqli_connect($servername, $username, $password, $database);



$con=mysqli_connect($host, $username, $password, $mydb);



if (!$con) {

    die("Connection failed: " . mysqli_connect_error());

}
$nResult = mysqli_query($con, $strQuery);


if (!$nResult) {
   // printf("Error: %s\n", mysqli_error($con));

echo "Error: ". $nResult;
    exit();
		}


		return mysqli_fetch_array($nResult);
		//return mysqli_fetch_assoc($nResult);
	}


 
	

function TextField($strLabel, $strField, $strValue, $nSize, $nMaxLength, $bPassword, $strExtra="")
	{
		echo "<tr>";
		echo "	<td nowrap>";
		echo		$strLabel;
		echo "	</td>";
		echo "	<td>";
		
		if($bPassword)
			echo "		<input type=password name=$strField value=\"$strValue\" size=$nSize maxlength=$nMaxLength $strExtra>";
		else
			echo "		<input type=text name=$strField value=\"$strValue\" size=$nSize maxlength=$nMaxLength $strExtra>";
		
		echo "	</td>";
		echo "</tr>";
	}

function OKCancelButtons()
	{
		echo "<tr>";
		echo "	<td></td>";
		echo "	<td>";
		echo "		<input type=submit value='   OK   '> <input type=submit value='Cancel' onClick='history.go(-1); return false;'>";
		echo "	</td>";
		echo "</tr>";
	}



function TextArea($strLabel, $strField, $strValue, $nRows, $nCols)
	{
		echo "<tr>";
		echo "	<td valign=top>";
		echo		$strLabel;
		echo "	</td>";
		echo "	<td>";		
		echo "		<textarea name=$strField rows=$nRows cols=$nCols>$strValue</textarea>";
		
		echo "	</td>";
		echo "</tr>";
	}

function HiddenField($strName, $strValue)
	{
		echo "<input type=hidden name='$strName' value='$strValue'>\r\n";
	}

/*
		the function creates a file upload widget on form.

		strLabel:			Label in left column
		strFileName:		File name	
	*/
	function FileUpload($strLabel, $strFileName)
	{
		echo "<tr>";
		echo "	<td valign=top>";
		echo		$strLabel;
		echo "	</td>";
		echo "	<td>";

		echo "<input type=file name='$strFileName' >";

		echo "	</td>";
		echo "</tr>";
	}

function RadioButtons($strLabel, $arrButtons, $strName, $nSelIndex = -1)
	{
		echo "<tr>";
		echo "	<td valign=top>";
		echo		$strLabel;
		echo "	</td>";
		echo "	<td>";

		for($i=0; $i<sizeof($arrButtons); $i++)
			if($i == $nSelIndex)
				echo "<input type=radio value=$i name=$strName checked>" . $arrButtons[$i] . "<br>";	
			else
				echo "<input type=radio value=$i name=$strName>" . $arrButtons[$i] . "<br>";	

		echo "	</td>";
		echo "</tr>";
	}

function AddDate($strLabel, $strField, $strValue, $nSize, $nMaxLength, $bPassword, $strExtra="")
	{
		
		echo "	<td nowrap>";
		echo		$strLabel;
		echo "	</td>";
		echo "	<td>";


			echo "		<input type=date name=$strField value=\"$strValue\" size=$nSize maxlength=$nMaxLength $strExtra>";

		
	}
function AddDate1($strLabel, $strField, $strValue, $nSize, $nMaxLength, $bPassword, $strExtra="")
	{
		echo		$strLabel;
	

			echo "		<input type=date name=$strField value=\"$strValue\" size=$nSize maxlength=$nMaxLength $strExtra>";

	}


	function AddDate2($strLabel, $strField, $strValue, $nSize, $nMaxLength, $bPassword, $strExtra="")
	{
		
		echo "	<td>";
		echo		$strLabel;
		echo "	</td>";
		echo "	<td colspan=3>";


			echo "		<input type=date name=$strField value=\"$strValue\" size=$nSize maxlength=$nMaxLength $strExtra>";

		
		echo "</tr>";
	}

function RecCount($strTable, $strCriteria = "")
	{		
		if(empty($strCriteria))
			$strQuery = "select count(*) as cnt from $strTable;";
		else
			$strQuery = "select count(*) as cnt from $strTable where $strCriteria;";

$h = new myclass();
$DB_DBName=$h->DB_DBName;
$DB_Server=$h->DB_Server;
$DB_Username=$h->DB_Username;
$DB_Password=$h->DB_Password;



 
 $connectme = new mysqli($DB_Server, $DB_Username, $DB_Password,$DB_DBName) or die("Connect failed: %s\n". $conn -> error); 


$result = mysqli_query($connectme, $strQuery);

$rstRow= mysqli_fetch_array($result);

return $rstRow["cnt"];
	
		
	}
?>