<?php
    include_once 'opendb.php';
    $filter="";
    $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $root_element = "SaleOrders"; //fruits
    $xml         .= "<".$root_element.">";
    $WebEntryNo=$_GET["WebEntry_No"];
    $VType=$_GET["VType"];
    
    if (!isset($WebEntryNo)){
       die("WebEntryno required");
    }
    $SubTable="sale_order_sub";
    if(Vtype=="Order"){
        $SubTable="sale_order_sub";
    } else if (Vtype=="Sales"){
        $SubTable="sales_sub";
    }
    
    $Qry="SELECT  Entry_No,prod_code,SL_No,Quantity,Rate,Unit,COST,pack_qty,discount,Foc_Qty,Foc_Unit,Loc_code,Batch,item_note FROM ".$SubTable." where Entry_no=".$WebEntryNo;
    $result = $conn->query($Qry) or die(mysqli_error($conn). $Qry); 
    
   header("Content-Type: application/xml");
   echo sqlToXml($result, "SaleOrder", "Rows");
   $conn->close(); 
  
function sqlToXml($queryResult, $rootElementName, $childElementName)
{ 
    $xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; 
    $xmlData .= "<" . $rootElementName . ">";
    
    while ($row = mysqli_fetch_assoc($queryResult))
    {
            $xmlData .= "<" . $childElementName . ">";
            foreach( $row as $field => $value) {
             
            // echo "<"."{$field}".">"."  - {$value}<br>";

                $xmlData .= "<{$field}>{$value}</{$field}>";
            }
            $xmlData .= "</" . $childElementName . ">"; 
    }
 
   
    $xmlData .= "</" . $rootElementName . ">"; 
 
    return $xmlData; 
}


    
    
?>