<?php
//require_once('Spreadsheet/Excel/Writer.php');
session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
//include ('function.inc.php');


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

/*
$workbook = new Spreadsheet_Excel_Writer();
$worksheet =& $workbook->addWorksheet('List of Fields');
if (PEAR::isError($worksheet)) {
    die($worksheet->getMessage());
}
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);


$columnWidth = 10;
 
$sheetTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'size'=>10));
 
$columnTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'top'=>1,
  'bottom'=>1 ,
  'size'=>15,
  'color'=>0x0A,
  ));
  
 
$regularleftFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ));
$regularrightFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ));  
$regularcenterFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'center'  )); 
  
$TitleFormat =& $workbook->addFormat(array('size'=>11,
  'align'=>'left',
  'bold'=>1
  ));

$boldleftFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ,
  'valign'=>'top'  ,
  'bold'=>1
  ));  
$boldrightFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ,
  'bold'=>1
  )); 
$boldcenterFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'center'  ,
  'valign'=>'top'  ,
  'bold'=>1
  ));    
     	

$worksheet->write(1,0,$CompanyName, $TitleFormat);
$worksheet->write(1,1,"List of Fields", $TitleFormat);
               
$worksheet->write(1,4,"Printed Date:", $boldleftFormat);
$worksheet->write(1,5,$printdate[2]."/".$printdate[1]."/".$printdate[0], $regularleftFormat);
$worksheet->write(2,4,"Time:", $boldleftFormat);		
$worksheet->write(2,5,$dt[1]." ".$dt[2], $regularleftFormat);
  

  
$RowNum    = 4;
$ColumnNum = 0;

$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
$worksheet->write($RowNum,$ColumnNum,"Voyage year", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Voyage number", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Voyage destination", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"B/L type", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"B/L number", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"B/L groupage number", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Voyage number", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Detail sequence.       ", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Source agent code", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Description of Goods", $boldleftFormat);
$ColumnNum += 1;


$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
$worksheet->write($RowNum,$ColumnNum,"Voyage year", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
$worksheet->write($RowNum,$ColumnNum,"Voyage number", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
$worksheet->write($RowNum,$ColumnNum,"Voyage destination", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
$worksheet->write($RowNum,$ColumnNum,"Source agent code", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
$worksheet->write($RowNum,$ColumnNum,"B/L type", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
$worksheet->write($RowNum,$ColumnNum,"B/L number", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
$worksheet->write($RowNum,$ColumnNum,"B/L groupage number", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Detail sequence", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Year", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Month", $boldleftFormat);
$ColumnNum += 1;

$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Day", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Hours", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Minutes", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
$worksheet->write($RowNum,$ColumnNum,"Second", $boldleftFormat);
$ColumnNum += 1;





$RowNum    = 5;
$ColumnNum = 0;*/
$myfile = fopen("sndbofl4.txt", "r") or die("Unable to open file!");

echo "<table border=1>";
echo"<tr>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Voyage year</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Voyage number</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Voyage destination</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>B/L type</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>B/L number</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'> B/L groupage number</th>
		
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Voyage number</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Detail sequence</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Source agent code</th>
		
		
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Description of Goods </th>
		";
		echo "<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Voyage year</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Voyage number</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Voyage destination</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Source agent code</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>B/L type</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>B/L number</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'> B/L groupage number</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Detail sequence</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Year</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Month</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Day</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Hours</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Minutes</th>
		<th style='font-size:16px;border-top: #A8A8A8 solid 1px;border-bottom: #A8A8A8 solid 1px;border-right: #A8A8A8 solid 1px;'>Second</th>
		
		</tr>";
while(!feof($myfile)) {
	$Recpos=1;
	$file=fgets($myfile);
	echo "<tr>";
//660
	while ($Recpos<20){

	$query="select Rec_pos,fieldlength,fieldfamily from sndbofl where  
	  rec_pos=$Recpos and fieldfamily='sndbofl4'";
	//echo $query;
	$subresult=mysql_query($query,$connection) or die(mysql_error());
	if($row=mysql_fetch_row($subresult)){
	
	 echo "<td>".substr($file,$Recpos-1,$row[1])."  </td>";

$Recpos=$Recpos+$row[1];
//$Recpos=$Recpos+10;

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

echo "</table>";
fclose($myfile);



	

function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}
//$workbook->send('ListOfFields.xls');
//$workbook->close();
?>