<?php

include "../config/conn.php";



if($_GET['tpos']=="PDA") {



error_reporting(E_ALL);

ini_set('display_errors', TRUE);

ini_set('display_startup_errors', TRUE);



if (PHP_SAPI == 'cli')

	die('This example should only be run from a Web Browser');



/** Include PHPExcel */

require_once dirname(__FILE__) . '/../lib/PHPExcel/IOFactory.php';



// Create new PHPExcel object

$objPHPExcel = PHPExcel_IOFactory::load("../file/templateDownloadTMA.xls");



$baseRow = 8;

$startRow = $baseRow;

$endRow = $baseRow;



$sql="SELECT DISTINCT ReceivedDate,jam_value AS ReceivedTime,UNIX_TIMESTAMP(CONCAT(ReceivedDate,'-',DATE_FORMAT(a.ReceivedTime,'%H:0'))) AS tgl,max(a.WLevel) AS WLevel, FORMAT(a.WLevel*100,0) AS klWLevel,ML

FROM ".$_GET['pos']." AS a

INNER JOIN m_jam AS b ON DATE_FORMAT(b.jam_value,'%H:0') = DATE_FORMAT(a.ReceivedTime,'%H:0')

WHERE 1=1 AND a.WLevel!=0 AND a.WLevel>=1 AND a.ReceivedDate>='".$_GET['tgls']."' AND a.ReceivedDate<='".$_GET['tglf']."' GROUP BY ReceivedDate,jam_value

ORDER BY a.ReceivedDate ASC,b.jam_value ASC";

$query2=mysql_query($sql);

$rowx = mysql_fetch_array($query2);



if (mysql_num_rows($query2) > 0 ) {

    $no = 1;

	$total=0;

	$objPHPExcel->setActiveSheetIndex(0)

          ->setCellValue('C'.'4', $_GET['pos'])

          ->setCellValue('C'.'5', $_GET['tgls']." s/d ".$_GET['tglf']);

	$query4=mysql_query($sql);

	while($row = mysql_fetch_array($query4) ) 

	{

		$objPHPExcel->setActiveSheetIndex(0)

            ->setCellValue('B'.$baseRow, $no)

            ->setCellValue('C'.$baseRow, $row['ReceivedDate'])

            ->setCellValue('D'.$baseRow, date('H:i',strtotime($row['ReceivedTime'])))

            ->setCellValue('E'.$baseRow, $row['WLevel']);

        $baseRow++;

        $no++;

    }

} else {

    

}

//die();

$objPHPExcel->setActiveSheetIndex(0);



$nvr=$_GET['pos'];

// Redirect output to a client’s web browser (Excel5)

header('Content-Type: application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="DataTMA'.$nvr.'.xls"');

header('Cache-Control: max-age=0');

// If you're serving to IE 9, then the following may be needed

header('Cache-Control: max-age=1');



// If you're serving to IE over SSL, then the following may be needed

header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past

header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified

header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1

header ('Pragma: public'); // HTTP/1.0



$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

exit; 



} else {



error_reporting(E_ALL);

ini_set('display_errors', TRUE);

ini_set('display_startup_errors', TRUE);



if (PHP_SAPI == 'cli')

	die('This example should only be run from a Web Browser');



/** Include PHPExcel */

require_once dirname(__FILE__) . '/../lib/PHPExcel/IOFactory.php';



// Create new PHPExcel object

$objPHPExcel = PHPExcel_IOFactory::load("../file/templateDownloadCH.xls");



$baseRow = 8;

$startRow = $baseRow;

$endRow = $baseRow;



/*$sql="SELECT DISTINCT ReceivedDate,ReceivedTime,UNIX_TIMESTAMP(CONCAT(ReceivedDate,'-',DATE_FORMAT(a.ReceivedTime,'%H:0'))) AS tgl,Rain,ML

FROM ".$_GET['pos']." AS a

WHERE 1=1 AND a.ReceivedDate>='".$_GET['tglf']."' AND a.ReceivedDate<='".$_GET['tglf']."' GROUP BY ReceivedDate,ReceivedTime

ORDER BY a.ReceivedDate ASC,a.ReceivedTime ASC";
*/
$sql="SELECT DISTINCT ReceivedDate,jam_value AS ReceivedTime,UNIX_TIMESTAMP(CONCAT(ReceivedDate,'-',DATE_FORMAT(a.ReceivedTime,'%H:0'))) AS tgl,Rain,ML
FROM ".$_GET['pos']." AS a
INNER JOIN m_jam AS b ON DATE_FORMAT(b.jam_value,'%H:0') = DATE_FORMAT(a.ReceivedTime,'%H:0')
WHERE 1=1 AND a.ReceivedDate>='".$_GET['tgls']."' AND a.ReceivedDate<='".$_GET['tglf']."' AND ML!='50' GROUP BY ReceivedDate,jam_value
ORDER BY a.ReceivedDate ASC,b.jam_value ASC";

$query2=mysql_query($sql);

$rowx = mysql_fetch_array($query2);



if (mysql_num_rows($query2) > 0 ) {

    $no = 1;

	$total=0;

	$objPHPExcel->setActiveSheetIndex(0)

          ->setCellValue('C'.'4', $_GET['pos'])

          ->setCellValue('C'.'5', $_GET['tgls']." s/d ".$_GET['tglf']);

	$query4=mysql_query($sql);

	while($row = mysql_fetch_array($query4) ) 

	{

		$objPHPExcel->setActiveSheetIndex(0)

            ->setCellValue('B'.$baseRow, $no)

            ->setCellValue('C'.$baseRow, $row['ReceivedDate'])

            ->setCellValue('D'.$baseRow, date('H:i',strtotime($row['ReceivedTime'])))

            ->setCellValue('E'.$baseRow, $row['Rain']);

        $baseRow++;

        $no++;

    }

} else {

    

}

//die();

$objPHPExcel->setActiveSheetIndex(0);



$nvr=$_GET['pos'];

// Redirect output to a client’s web browser (Excel5)

header('Content-Type: application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="DataPCH'.$nvr.'.xls"');

header('Cache-Control: max-age=0');

// If you're serving to IE 9, then the following may be needed

header('Cache-Control: max-age=1');



// If you're serving to IE over SSL, then the following may be needed

header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past

header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified

header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1

header ('Pragma: public'); // HTTP/1.0



$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

exit;	

}

?>