<?php
    include_once 'opendb.php';
   
    $filter="";
    $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $root_element = "Requests"; //fruits
    $xml         .= "<".$root_element.">";
    $pendingonly=0;
     if (isset($_GET["pendingonly"])){
         $pendingonly=$_GET["pendingonly"];
     }
     if($pendingonly==1){
          $filter= " where req_status =0";
     }
     else {
         $filter= " where req_status >=0";
     }
    
    if (isset($_GET["fromdate"]))
    {
         $filter.=" and request_time >='" . $_GET["fromdate"]."'";
    }
    $CompID=$_GET["comp_id"]; 
    $filter.=" and comp_id=" .$CompID;
    
    $Qry="SELECT request_id,client_app_id,Notes,request_type,request_time,Counter_Code,Cust_Code,Reply_message,req_status,reply_time,file_name from client_requests  ";
    
    $Qry.=$filter . " order by request_id ";  
   // echo $Qry;
    $result = $conn->query($Qry) or die(mysql_error());
   header("Content-Type: application/xml");
   echo sqlToXml($result, "Requests", "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; 
}


    
    
?>