<?php
require_once('config.php');
$error_message;
$valid = true;


$sql = "SELECT 	job_mst.job_mst_job_cd,   
				job_mst.job_mst_desc,   			
				sec_mst.sec_no, 
				sec_mst.sec_desc,
				stp_mst.stp_no,
				stp_mst.stp_desc,
				stp_mst.stp_datatype,
				stp_mst.RowID
		FROM 	job_mst,
					sec_mst,
					stp_mst
		WHERE 	( job_mst.site_cd = sec_mst.site_cd )
		AND		( job_mst.site_cd = stp_mst.site_cd )
		AND		( job_mst.RowID = sec_mst.mst_RowID ) 
		AND		( stp_mst.sec_RowID = sec_mst.RowID )
		AND		( job_mst.RowID = stp_mst.mst_RowID )
		AND		( job_mst.site_cd = '".$_REQUEST['site_cd']."'  ) 
		AND  	( job_mst.job_mst_job_cd = ".$_REQUEST['job_mst_job_cd']);
			
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Work Order Check List)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
}

$json = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
     $json[] = $row;
     }
} while ( sqlsrv_next_result($stmt) );

if ($valid) {
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
	
	returnData($json);	
}

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Select Successfully',
	'data' => $json);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message,
	'data' => $json);
	
	echo json_encode($returnData);
	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
		
	exit();
}




		


?>