<?php 
// session_start();
//設定語言
if (isset($_SESSION['lang'])){
	if ($_SESSION['lang'] == "chi"){
		require "language/chi.php";
	} else {
		require "language/eng.php";
	}
	$lang = $_SESSION['lang'];
} else {
	require "language/chi.php";
}
//完成設定語言
require "common.php";
// require_once 'PHPExcel.php';

//獲取所有POST
foreach ($_POST as $key => $value) 	{ 
	$$key = $value;    //2個$$代表轉為VARIABLE
}
foreach ($_GET as $key => $value) 	{ 
	$$key = $value;    //2個$$代表轉為VARIABLE
}
//完成獲取所有POST
if(isset($_SESSION['STAFFCODE'])){
	$staffcode = $_SESSION['STAFFCODE'];
}

//設定學年
$findmax = mysqli_query($splink,"Select MAX(SCHYEAR) FROM SCHYEAR");
while ($getmax = mysqli_fetch_row($findmax)){
	$schyr = $getmax[0];
}
if (!isset($targetschoolyear)){
	$targetschoolyear = $schyr;
}
//完成設定學年

//php exportdatatonas
if (isset($exporttodayyeardatatonas)){
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header('Content-Description: File Transfer');
	header("Content-type: text/csv");
	header("Content-Disposition: attachment; filename=".$todayschyr."_export_from_Sportday_system_to_nas.txt");
	header("Expires: 0");
	header("Pragma: public");
	$txt = fopen( 'php://output', 'W') or die("Unable to ope file!");
	// fputcsv($file, array ( "Class","ClassNumber","MessageTitle","MessageContent\n"));
	fwrite($txt, "\xEF\xBB\xBF");

	fwrite($txt,"TRUNCATE APPLICATION_COMPETITION\n");	//schyr
	$sql = mysqli_query($splink,"Select keyid, SCHYEAR, STUID, CHOICE, COMPETITION_ID, ATTENDANCE from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\"");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 6 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into APPLICATION_COMPETITION (  keyid, SCHYEAR, STUID, CHOICE, COMPETITION_ID, ATTENDANCE  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE APPLICATION_STUDENTWORKER\n");	//schyr
	$sql = mysqli_query($splink,"Select keyid, SCHYEAR, STUID, choice, sw_id, approved, approvedby, exported, PREF_ID from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$todayschyr."\" AND approved=\"1\"");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 9 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into APPLICATION_STUDENTWORKER (  keyid, SCHYEAR, STUID, choice, sw_id, approved, approvedby, exported, PREF_ID  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE BEST_RECORD\n");
	$sql = mysqli_query($splink,"Select keyid, SCHYEAR, COMPETITION_ID, NAME, HOUSE, RECORD, STANDARD from BEST_RECORD");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 7 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into BESTRECORD ( keyid, SCHYEAR, COMPETITION_ID, NAME, HOUSE, RECORD, STANDARD ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE COMPETITIONITEM\n");		//no schyr
	$sql = mysqli_query($splink,"Select keyid, Allowapply, CH_DES, EN_DES, GRADE, TRACKFIELD, SEX, SETLANE, UNIT, INTERHOUSE, INTERCLASS,OPENSCH, STANDARD, MEASURE from COMPETITIONITEM");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 14 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into COMPETITIONITEM (  keyid, Allowapply, CH_DES, EN_DES, GRADE, TRACKFIELD, SEX, SETLANE, UNIT, INTERHOUSE, INTERCLASS,OPENSCH, STANDARD, MEASURE  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE COMPETITION_DATEID\n");  //no schyr
	$sql = mysqli_query($splink,"Select keyid, COMPETITION_ID, DATE, ORDER_ID, HeatorFinal,EVENTTIME from COMPETITION_DATEID");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 5 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into COMPETITION_DATEID (  keyid, COMPETITION_ID, DATE, ORDER_ID, HeatorFinal,EVENTTIME  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE CONFIRMED_RESULT\n"); //no need import
	fwrite($txt,"TRUNCATE gradeyear\n");  //schyr
	$sql = mysqli_query($splink,"Select keyid, SCHYEAR, grade, gradeyear from gradeyear where SCHYEAR=\"".$todayschyr."\"");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 4 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into gradeyear (  STUID  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE HEAT_RESULT\n"); //no need import
	fwrite($txt,"TRUNCATE HOUSE_MEMBER\n"); //no schyr
	$sql = mysqli_query($splink,"Select STUID, HOUSE from HOUSE_MEMBER");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 2 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into HOUSE_MEMBER (  STUID, HOUSE  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE HOUSE_SCORE\n"); //no need import
	fwrite($txt,"TRUNCATE INTERHOUSE_COMPETITION\n");
	$sql = mysqli_query($splink,"Select keyid, COMPETITION_ID, SCHHOUSE, TEAM, STUID, LANE_NO from INTERHOUSE_COMPETITION");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 6 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into INTERHOUSE_COMPETITION (  keyid, COMPETITION_ID, SCHHOUSE, TEAM, STUID, LANE_NO  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE INTERHOUSE_RESULT\n"); //no need import
	fwrite($txt,"TRUNCATE moduleenable\n");
	$sql = mysqli_query($splink,"Select keyid, module, apenable, Start_date, End_date from moduleenable");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 5 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into moduleenable (  keyid, module, apenable, Start_date, End_date  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE moduleuser\n");
	$sql = mysqli_query($splink,"Select keyid, modulephp, displayorder, modulename, users, stuid from moduleuser");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 6 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into moduleuser (  keyid, modulephp, displayorder, modulename, users, stuid  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE NEW_RECORD\n"); //no need import
	fwrite($txt,"TRUNCATE SCHYEAR\n"); //schyr
	$sql = mysqli_query($splink,"Select keyid, SCHYEAR, activeyear, EVENT_CHNAME, EVENT_ENNAME from SCHYEAR where SCHYEAR=\"".$todayschyr."\"");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 5 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into SCHYEAR (  keyid, SCHYEAR, activeyear, EVENT_CHNAME, EVENT_ENNAME  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE staffduty\n");
	$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, STAFF from staffduty");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 4 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into staffduty (  keyid, CH_DES, EN_DES, STAFF  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE studallow\n"); // no schyr
	$sql = mysqli_query($splink,"Select STUID from studallow");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 1 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into studallow (  STUID  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE studentinfo\n");
	$sql = mysqli_query($splink,"Select CHNAME, ENNAME, SEX, CLASSCODE, CLASSNO, DOB, REGNO, SCHHOUSE, SCHLEAVERSIGN, STUID, SUID, password from studentinfo");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 12 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into studentinfo (CHNAME, ENNAME, SEX, CLASSCODE, CLASSNO,DOB,REGNO, SCHHOUSE, SCHLEAVERSING, STUID, SUID, password ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE studentworker\n");
	$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, NEED_NUM, REQUIRE_SEX, CH_REMARK, EN_REMARK, allowapply, activated from studentworker");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 9 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into studentworker (  keyid, CH_DES, EN_DES, NEED_NUM, REQUIRE_SEX, CH_REMARK, EN_REMARK, allowapply, activated  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE STU_SCORE\n"); //no need import
	fwrite($txt,"TRUNCATE TB_STU_STUDENT\n"); //no schyr
	$sql = mysqli_query($splink,"Select REGNO, STUID, password from TB_STU_STUDENT");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 3 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into TB_STU_STUDENT ( REGNO, STUID, password  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE teacherinfo\n");
	$sql = mysqli_query($splink,"Select STAFFCODE, STAFFUID, CH_NAME, EN_NAME, stafftitle, password from teacherinfo");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 6 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into teacherinfo (  STAFFCODE, STAFFUID, CH_NAME, EN_NAME, stafftitle, password  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE TRACKEVENT_FINAL_LANE\n"); //no need import
	fwrite($txt,"TRUNCATE TRACKEVENT_HEAT_LANE\n");
	$sql = mysqli_query($splink,"Select keyid, EVENT_ID, COMPETITION_ID, STUID, LANE_NO, ATTENDANCE, RESULT from TRACKEVENT_HEAT_LANE");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 7 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into TRACK_HEAT_LANE (  keyid, EVENT_ID, COMPETITION_ID, STUID, LANE_NO, ATTENDANCE, RESULT  ) VALUES ( ".$str." )\n");
	}
	fclose($txt);
	
	exit;
			
}
//finish php exportdatatonas


//php export data from NAS
if (isset($exporttoddayyeardatafromnas)){
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header('Content-Description: File Transfer');
	header("Content-type: text/csv");
	header("Content-Disposition: attachment; filename=".$todayschyr."_export_from_nas_to_sportday_system.txt");
	header("Expires: 0");
	header("Pragma: public");
	$txt = fopen( 'php://output', 'W') or die("Unable to ope file!");
	// fputcsv($file, array ( "Class","ClassNumber","MessageTitle","MessageContent\n"));
	fwrite($txt, "\xEF\xBB\xBF");

//	fwrite($txt,"Delete from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" \n");	//schyr
	$sql = mysqli_query($splink,"Select SCHYEAR, STUID, COMPETITION_ID, ATTENDANCE from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\"");
	while ($row = mysqli_fetch_row($sql)){
		fwrite($txt,"Update APPLICATION_COMPETITION SET ATTENDANCE = \"".$row[3]."\" WHERE SCHYEAR=\"".$row[0]."\" AND STUID=\"".$row[1]."\" AND COMPETITION_ID=\"".$row[2]."\" \n");
	}
//	fwrite($txt,"Delete from APPLICATION_STUDENTWORKER where SCHYEAR=\"\n");	//schyr
	$sql = mysqli_query($splink,"Select SCHYEAR, STUID, sw_id, PREF_ID from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$todayschyr."\" AND approved=\"1\"");
	while ($row = mysqli_fetch_row($sql)){
		fwrite($txt,"Update APPLICATION_STUDENTWORKER SET PREF_ID=\"".$row[3]."\" WHERE SCHYEAR=\"".$row[0]."\" AND STUID=\"".$row[1]."\" AND sw_id=\"".$row[2]."\" \n");
	}
	fwrite($txt,"TRUNCATE BEST_RECORD\n");
	$sql = mysqli_query($splink,"Select keyid, SCHYEAR, COMPETITION_ID, NAME, HOUSE, RECORD, STANDARD from BEST_RECORD");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 7 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into BESTRECORD ( keyid, SCHYEAR, COMPETITION_ID, NAME, HOUSE, RECORD, STANDARD ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"Delete CONFIRMED_RESULT where SCHYEAR=\"".$todayschyr."\" \n"); // schyr
	$sql = mysqli_query($splink,"Select SCHYEAR, COMPETITION_ID, EVENT_ID, STUID,HeatorFinal, RANK, RESULT, is_NEWRECORD from CONFIRMED_RESULT where SCHYEAR=\"".$todayschyr."\"");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 8 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into BESTRECORD ( SCHYEAR, COMPETITION_ID, EVENT_ID, STUID,HeatorFinal, RANK, RESULT, is_NEWRECORD ) VALUES ( ".$str." )\n");
	}

	fwrite($txt,"TRUNCATE HEAT_RESULT\n"); //no need import
	$sql = mysqli_query($splink,"Select COMPETITION_ID, STUID, RESULT from HEAT_RESULT");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 3 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into HEAT_RESULT ( COMPETITION_ID, STUID, RESULT) VALUES ( ".$str." )\n");
	}
	
	
	fwrite($txt,"TRUNCATE HOUSE_MEMBER\n"); //no schyr
	
	fwrite($txt,"Delete HOUSE_SCORE where SCHYEAR=\"".$todayschyr."\" \n"); //schyr
	$sql = mysqli_query($splink,"Select SCHYEAR, SCHHOUSE, GRADE, SEX, SCORE from HOUSE_SCORE where SCHYEAR=\"".$todayschyr."\" ");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 5 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into HOUSE_SCORE ( SCHYEAR, SCHHOUSE, GRADE, SEX, SCORE) VALUES ( ".$str." )\n");
	}
	
	fwrite($txt,"TRUNCATE INTERHOUSE_COMPETITION\n");
	
	fwrite($txt,"DELETE INTERHOUSE_RESULT where SCHYEAR=\"".$todayschyr."\" \n"); //no need import
	$sql = mysqli_query($splink,"Select SCHYEAR, COMPETITION_ID, SCHHOUSE, TEAM, RESULT, ATTENDANCE, RANK, is_NEWRECORD from INTERHOUSE_RESULT where SCHYEAR=\"".$todayschyr."\" ");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 8 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into INTERHOUSE_COMPETITION ( SCHYEAR, COMPETITION_ID, SCHHOUSE, TEAM, RESULT, ATTENDANCE, RANK, is_NEWRECORD ) VALUES ( ".$str." )\n");
	}

	fwrite($txt,"Update moduleuser SET stuid=\"\" \n");
	fwrite($txt,"Delete NEW_RECORD where SCHYEAR=\"".$todayschyr."\" \n"); //no need import
	$sql = mysqli_query($splink,"Select SCHYEAR, COMPETITION_ID, STUID, RECORD from NEW_RECORD where SCHYEAR=\"".$todayschyr."\" ");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 4 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into NEW_RECORD ( SCHYEAR, COMPETITION_ID, STUID, RECORD ) VALUES ( ".$str." )\n");
	}

	fwrite($txt,"TRUNCATE studallow\n"); // no schyr
	fwrite($txt,"Delete STU_SCORE where SCHYEAR=\"".$todayschyr."\" \n"); //
	$sql = mysqli_query($splink,"Select SCHYEAR, STUID, SCORE from STU_SCORE where SCHYEAR=\"".$todayschyr."\" ");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 3 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into STU_SCORE ( SCHYEAR, STUID, SCORE ) VALUES ( ".$str." )\n");
	}
	
	fwrite($txt,"TRUNCATE TB_STU_STUDENT\n"); //no schyr
	fwrite($txt,"TRUNCATE TRACKEVENT_FINAL_LANE\n"); //no need import
	$sql = mysqli_query($splink,"Select COMPETITION_ID, STUID, LANE_NO, ATTENDANCE, RESULT from TRACKEVENT_FINAL_LANE");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 5 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into TRACKEVENT_FINAL_LANE ( COMPETITION_ID, STUID, LANE_NO, ATTENDANCE, RESULT ) VALUES ( ".$str." )\n");
	}

	fwrite($txt,"TRUNCATE TRACKEVENT_HEAT_LANE\n");
	$sql = mysqli_query($splink,"Select keyid, EVENT_ID, COMPETITION_ID, STUID, LANE_NO, ATTENDANCE, RESULT from TRACKEVENT_HEAT_LANE");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 7 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into TRACK_HEAT_LANE (  keyid, EVENT_ID, COMPETITION_ID, STUID, LANE_NO, ATTENDANCE, RESULT  ) VALUES ( ".$str." )\n");
	}
	fclose($txt);
/*	無需匯出的資料
	fwrite($txt,"TRUNCATE COMPETITIONITEM\n");		//no schyr
	$sql = mysqli_query($splink,"Select keyid, Allowapply, CH_DES, EN_DES, GRADE, TRACKFIELD, SEX, SETLANE, UNIT, INTERHOUSE, INTERCLASS,OPENSCH, STANDARD, MEASURE from COMPETITIONITEM");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 14 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into COMPETITIONITEM (  keyid, Allowapply, CH_DES, EN_DES, GRADE, TRACKFIELD, SEX, SETLANE, UNIT, INTERHOUSE, INTERCLASS,OPENSCH, STANDARD, MEASURE  ) VALUES ( ".$str." )\n");
	} 
	fwrite($txt,"TRUNCATE COMPETITION_DATEID\n");  //no schyr
	$sql = mysqli_query($splink,"Select keyid, COMPETITION_ID, DATE, ORDER_ID, HeatorFinal,EVENTTIME from COMPETITION_DATEID");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 5 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into COMPETITION_DATEID (  keyid, COMPETITION_ID, DATE, ORDER_ID, HeatorFinal,EVENTTIME  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE gradeyear\n");  //schyr
	$sql = mysqli_query($splink,"Select keyid, SCHYEAR, grade, gradeyear from gradeyear where SCHYEAR=\"".$todayschyr."\"");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 4 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into gradeyear (  STUID  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE moduleenable\n");
	$sql = mysqli_query($splink,"Select keyid, module, apenable, Start_date, End_date from moduleenable");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 5 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into moduleenable (  keyid, module, apenable, Start_date, End_date  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE SCHYEAR\n"); //schyr
	$sql = mysqli_query($splink,"Select keyid, SCHYEAR, activeyear, EVENT_CHNAME, EVENT_ENNAME from SCHYEAR where SCHYEAR=\"".$todayschyr."\"");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 5 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into SCHYEAR (  keyid, SCHYEAR, activeyear, EVENT_CHNAME, EVENT_ENNAME  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE staffduty\n");
	$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, STAFF from staffduty");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 4 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into staffduty (  keyid, CH_DES, EN_DES, STAFF  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE studentinfo\n");
	$sql = mysqli_query($splink,"Select CHNAME, ENNAME, SEX, CLASSCODE, CLASSNO, DOB, REGNO, SCHHOUSE, SCHLEAVERSIGN, STUID, SUID, password from studentinfo");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 12 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into studentinfo (CHNAME, ENNAME, SEX, CLASSCODE, CLASSNO,DOB,REGNO, SCHHOUSE, SCHLEAVERSING, STUID, SUID, password ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE studentworker\n");
	$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, NEED_NUM, REQUIRE_SEX, CH_REMARK, EN_REMARK, allowapply, activated from studentworker");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 9 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into studentworker (  keyid, CH_DES, EN_DES, NEED_NUM, REQUIRE_SEX, CH_REMARK, EN_REMARK, allowapply, activated  ) VALUES ( ".$str." )\n");
	}
	$sql = mysqli_query($splink,"Select REGNO, STUID, password from TB_STU_STUDENT");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 3 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into TB_STU_STUDENT ( REGNO, STUID, password  ) VALUES ( ".$str." )\n");
	}
	fwrite($txt,"TRUNCATE teacherinfo\n");
	$sql = mysqli_query($splink,"Select STAFFCODE, STAFFUID, CH_NAME, EN_NAME, stafftitle, password from teacherinfo");
	while ($row = mysqli_fetch_row($sql)){
		$x = 0;
		$y = 6 ; //no of field
		$str = "";
		while ($x < $y ) {
			$str .= " \"".$row[$x]."\" ,";
			$x++;
		}
		$str = substr($str,0,-1);
		fwrite($txt,"Insert into teacherinfo (  STAFFCODE, STAFFUID, CH_NAME, EN_NAME, stafftitle, password  ) VALUES ( ".$str." )\n");
	}
*/
	
	exit;
			
}
//finish php exportdatatoits




?>