<html>
	<body style="background-color: #f9f8c9;">
	</body>
</html>

<?php
/////////////////////////////////////////////////
// START
$year = $_POST["year"];
$month = $_POST["month"];
$day = $_POST["day"];

/*
echo '###### Start query <br>';
echo("year\t ".$year."<br>");
echo("month\t ".$month."<br>");
echo("day\t ".$day."<br>");
 */

/* Connect to server */
$link = mysql_connect ('localhost', 'linpuscd', '9i)e!aK72UI7');
if (!$link){
	die ('@@@ Could not connect:' . mysql_error());
}

/* Make linpuscd_LiveUpdate as the current database */
$db_selected = mysql_select_db ('linpuscd_LiveUpdate', $link);
if (!$db_selected){
	die ('@@@ Can not use linpuscd_LiveUpdate: <br>' . mysql_error());
}
echo '<b>Connected successfully <b><br>';
echo '<br>';

/* Perform SQL query */
$len_year = strlen($year);
$len_month = strlen($month);
$len_day = strlen($day);

/*
echo("len_year\t ".$len_year."<br>");
echo("len_month\t ".$len_month."<br>");
echo("len_day\t ".$len_day."<br>");
 */

if (($len_year == 0)&&($len_month == 0)&&($len_day == 0)){
	///// get all update information
	echo "Select all update record !"."<br>";
	$query = 'SELECT * FROM update_info_enhance';

	$result = mysql_query($query);
	if (!result){
		die ('@@@ Invalid query: ' . mysql_error());
	}

	/* Count row */
	$num = mysql_num_rows($result);
	if ($num == 0){
		die ('!No update record' . mysql_error());
	}
//	echo "rows_num: $num";

	/* Use html display the result which perform SQL query */
	echo "<table width=\"80%\" border=\"1\">\n";

	echo "<tr>\n";
	echo "<td><b>Id</b></td>";
	echo "<td><b>SN</b></td>";
	echo "<td><b>Update Date</b></td>";
	echo "<td><b>Update Time</b></td>";
	echo "<td><b>Download Time</b></td>";
	echo "<td><b>Download Size</b></td>";
	echo "<td><b>Download List</b></td>";
	echo "</tr>\n";

	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
		echo "<tr>";
		foreach ($line as $col_value){
			echo "<td>$col_value</td>";
		}
		echo "</tr>";
		//	echo "<br>";
	}
	echo "</table><br><br>";

	/* Free result */
	$free_result = mysql_free_result($result);
	if (!$free_result){
		die ('Free memory: ' . mysql_error());
	}
	//echo "###### Success to free result<br>";


}elseif(($len_year != 0)&&($len_month == 0)&&($len_day == 0)){
	///// get info for months of year
	echo "Select year: "."<b>$year</b>"."<br>";
	echo "<table width=\"80%\" border=\"1\">\n";
	echo "<tr>\n";
	echo "<td><b>Month</b></td>";
	echo "<td><b>People</b></td>";
	echo "<td><b>Update Times</b></td>";
	echo "<td><b>Update Size (MB)</b></td>";
	echo "</tr>\n";

	for ($i=1, $j=2; $i <= 12; $i++,$j++){
		if ($i < 10){
			$query = sprintf("SELECT sn, update_size FROM update_info_enhance where update_date >= '%d-0%d-01' and update_date < '%d-0%d-01'", $year, $i, $year, $j);
			$query_sn = sprintf("SELECT distinct sn FROM update_info_enhance where update_date >= '%d-0%d-01' and update_date < '%d-0%d-01'", $year, $i, $year, $j);
		} else{
			$query = sprintf("SELECT sn, update_size FROM update_info_enhance where update_date >= '%d-%d-01' and update_date < '%d-%d-01'", $year, $i, $year, $j);
			$query_sn = sprintf("SELECT distinct sn FROM update_info_enhance where update_date >= '%d-%d-01' and update_date < '%d-0%d-01'", $year, $i, $year, $j);
		}

		echo "<tr>";
		switch ($i) {
		case 1:
			echo "<td>Jan</td>";
			break;
		case 2:
			echo "<td>Feb</td>";
			break;
		case 3:
			echo "<td>Mar</td>";
			break;
		case 4:
			echo "<td>Apr</td>";
			break;
		case 5:
			echo "<td>May</td>";
			break;
		case 6:
			echo "<td>Jun</td>";
			break;
		case 7:
			echo "<td>Jul</td>";
			break;
		case 8:
			echo "<td>Aug</td>";
			break;
		case 9:
			echo "<td>Sep</td>";
			break;
		case 10:
			echo "<td>Oct</td>";
			break;
		case 11:
			echo "<td>Nov</td>";
			break;
		case 12:
			echo "<td>Dec</td>";
			break;
		default:
			break;
		}

		/* Get the number of update people*/
		$result = mysql_query($query_sn);
		$people = mysql_num_rows($result);
		if ($people == 0){
			//  die ('@@@ No people updated:' . mysql_error());
		}
		echo "<td>$people</td>";
		mysql_free_result($result);

		$result = mysql_query($query);
		/* Update Times */
		$update_times = 0;
		$update_times = mysql_num_rows($result);
		if ($update_times == 0){
			// die ('@@@ No Updated:' . mysql_error());
		}
		echo "<td>$update_times</td>";
		$total_download_times += $update_times;

		/* Get update_size/month */
		$down_size = 0;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$down_size += $row["update_size"];
		}
		$update_size = (int)($down_size/1024);
		echo "<td>$update_size</td>";
		$down_size = 0;
		$total_download_size += $update_size;
		mysql_free_result($result);
		echo "</tr>";
		//	echo "<br>";
	}
	echo "<tr>";
	echo "<td><b>total</b></td>";
	echo "<td><b>/</b></td>";
	echo "<td><b>$total_download_times<b></td>";
	echo "<td><b>$total_download_size<b></td>";
	echo "</tr>";
	echo "</table><br><br>";


}elseif(($len_year != 0)&&($len_month != 0)&&($len_day == 0)){
	///// get info for days of month of year
	echo "Select: ".$year."/".$month."<br>";
	echo "<table width=\"80%\" border=\"1\">\n";
	echo "<tr>\n";
	echo "<td><b>Day</b></td>";
	echo "<td><b>People</b></td>";
	echo "<td><b>Update Times</b></td>";
	echo "<td><b>Update Size (MB)</b></td>";
	echo "</tr>\n";

	for ($i=1, $j=2; $i <= 12; $i++,$j++){
		if ($i < 10){
			$query = sprintf("SELECT sn, update_size FROM update_info_enhance where update_date >= '%d-0%d-01' and update_date < '%d-0%d-01'", $year, $i, $year, $j);
			$query_sn = sprintf("SELECT distinct sn FROM update_info_enhance where update_date >= '%d-0%d-01' and update_date < '%d-0%d-01'", $year, $i, $year, $j);
		} else{
			$query = sprintf("SELECT sn, update_size FROM update_info_enhance where update_date >= '%d-%d-01' and update_date < '%d-%d-01'", $year, $i, $year, $j);
			$query_sn = sprintf("SELECT distinct sn FROM update_info_enhance where update_date >= '%d-%d-01' and update_date < '%d-0%d-01'", $year, $i, $year, $j);
		}

		echo "<tr>";
//		echo "<td>$i</td>";
		/* Get the number of update people*/
		$result = mysql_query($query_sn);
		$people = mysql_num_rows($result);
		if ($people == 0){
			//  die ('@@@ No people updated:' . mysql_error());
		}
//		echo "<td>$people</td>";
		echo "</tr>\n";

		for ($i=1; $i <= 31; $i++){
			if ($i < 10){
				if ($month < 10){
					$query = sprintf("SELECT * FROM update_info_enhance where update_date='%d-0%d-0%d'", $year, $month, $i);
					$query_sn = sprintf("SELECT distinct sn FROM update_info_enhance where update_date='%d-0%d-0%d'", $year, $month, $i);
				}else{
					$query = sprintf("SELECT * FROM update_info_enhance where update_date='%d-%d-0%d'", $year, $month, $i);
					$query_sn = sprintf("SELECT distinct sn FROM update_info_enhance where update_date='%d-%d-0%d'", $year, $month, $i);
				}
			}else{
				if ($month < 10){
					$query = sprintf("SELECT * FROM update_info_enhance where update_date='%d-0%d-%d'", $year, $month, $i);
					$query_sn = sprintf("SELECT distinct sn FROM update_info_enhance where update_date='%d-0%d-%d'", $year, $month, $i);
				}else{
					$query = sprintf("SELECT * FROM update_info_enhance where update_date='%d-%d-%d'", $year, $month, $i);
					$query_sn = sprintf("SELECT distinct sn FROM update_info_enhance where update_date='%d-%d-%d'", $year, $month, $i);
				}
			}
			echo "<tr>";
			echo "<td>$i</td>";
			/* Get the number of update people*/
			$result = mysql_query($query_sn);
			$people = mysql_num_rows($result);
			if ($people == 0){
				// die ('@@@ No people updated:' . mysql_error());
			}
			echo "<td>$people</td>";
			mysql_free_result($result);

			$result = mysql_query($query);
			/* Update Times */
			$update_times = 0;
			$update_times = mysql_num_rows($result);
			if ($update_times == 0){
				// die ('@@@ No Updated:' . mysql_error());
			}
			echo "<td>$update_times</td>";
			$total_download_times += $update_times;

			/* Get update_size/month */
			$down_size = 0;
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
				$down_size += $row["update_size"];
			}
			$update_size = (int)($down_size/1024);
			$total_download_size += $update_size; 
			echo "<td>$update_size</td>";
			$down_size = 0;
			mysql_free_result($result);

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

		/* Total for particular month */
		echo "<tr>";
		echo "<td><b>total</b></td>";
		echo "<td><b>/</b></td>";
		echo "<td><b>$total_download_times</b></td>";
		echo "<td><b>$total_download_size</b></td>";
		echo "</tr>";

		echo "</table><br><br>";

	}
}elseif(($len_year != 0)&&($len_month != 0)&&($len_day != 0)){
	///// day info
	echo "Select: ".$year."/".$month."/".$day."<br>";
	if ($day<10)
		$query = sprintf("SELECT * FROM update_info_enhance where update_date='%d-%d-0%d'", $year, $month, $day);
	else
		$query = sprintf("SELECT * FROM update_info_enhance where update_date='%d-%d-%d'", $year, $month, $day);

	// start query with row!
	$result = mysql_query($query);
	if (!result){
		die ('@@@ Invalid query: ' . mysql_error());
	}

	/* Count row */
	$num = mysql_num_rows($result);
	if ($num == 0){
		die ('!No update record' . mysql_error());
	}
//	echo "rows_num: $num";

	/* Use html display the result which perform SQL query */
	echo "<table width=\"80%\" border=\"1\">\n";

	echo "<tr>\n";
	echo "<td><b>Id</b></td>";
	echo "<td><b>SN</b></td>";
	echo "<td><b>Update Date</b></td>";
	echo "<td><b>Update Time</b></td>";
	echo "<td><b>Download Time</b></td>";
	echo "<td><b>Download Size (KB)</b></td>";
	echo "<td><b>Download List</b></td>";
	echo "</tr>\n";

	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
		echo "<tr>";
		foreach ($line as $col_value){
			echo "<td>$col_value</td>";
		}
		echo "</tr>";
		//	echo "<br>";
	}
	echo "</table><br><br>";

	/* Free result */
	$free_result = mysql_free_result($result);
	if (!$free_result){
		die ('Free memory: ' . mysql_error());
	}
	//echo "###### Success to free result<br>";
}else{
	echo "error! Please check you input";
}

/* Closed connect */
mysql_close ($link);

//END
///////////////////////////////////////////////////////////////////
?>
