<?php
require_once('open_connection.php');


$SQL = "SELECT tbl_dir_fnb_customer.cust_id,tbl_dir_fnb_customer.cust_name,tbl_dir_fnb_quotation.quotation_id,tbl_dir_fnb_quotation.quotation_status,
                   tbl_dir_fnb_quotation.quotation_date_from, tbl_dir_fnb_quotation.quotation_date_to,
				   tbl_dir_fnb_quotation.quotation_remarks, tbl_dir_fnb_quotation.quotation_status,tbl_dir_fnb_item.item_total
                   FROM `tbl_dir_fnb_quotation` 
				   LEFT JOIN tbl_dir_fnb_item
				   ON tbl_dir_fnb_item.quotation_id=tbl_dir_fnb_quotation.quotation_id
				   LEFT JOIN tbl_dir_fnb_customer
				   ON tbl_dir_fnb_quotation.cust_id=tbl_dir_fnb_customer.cust_id
				   order by  tbl_dir_fnb_quotation.quotation_id,quotation_id ASC";
$header = '';
$result ='';
$exportData = mysql_query ($SQL ) or die ( "Sql error : " . mysql_error( ) );
 
$fields = mysql_num_fields ( $exportData );
 
for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $exportData , $i ) . "\t";
}
 
while( $row = mysql_fetch_row( $exportData ) )
{
    $line = '';
    foreach( $row as $value )
    {                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $result .= trim( $line ) . "\n";
}
$result = str_replace( "\r" , "" , $result );
 
if ( $result == "" )
{
    $result = "\nNo Record(s) Found!\n";                        
}
 
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=exportquotation.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$result";
 
?>