<?php
    include_once 'opendb.php';
   
    $filter="";
    $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $root_element = "Visits"; //fruits
    $xml         .= "<".$root_element.">";
    $fromdate=$_GET["fromdate"];
    $todate=$_GET["todate"];
    $filter.=" where (visit_date between '$fromdate' and '$todate') ";

    $CompID=$_GET["comp_id"]; 
    $filter.=" and comp_id=" .$CompID;
    if (!empty($_GET["clientcode"])){
         $filter.=" and client_code like '".$_GET["clientcode"]."' ";
    }
    $Qry="SELECT  `cust_code`,`cust_name`, `address`, `area`, `phone`, `tax_reg_no`, `client_code`, `visit_date`, `latitude`, `longitude`, `distance`, `accuracy`, `upd_time`, `notes` FROM `customer_visits` ";
    
    $Qry.=$filter . " order by web_entry_no ";  
   // echo $Qry;
    $result = $conn->query($Qry) or die(mysql_error());
   header("Content-Type: application/xml");
   echo sqlToXml($result, "Visits", "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; 
}


    
    
?>