<?php
    include_once 'opendb.php';
   
    $filter="";
    $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $root_element = "Visits"; //fruits
    $xml         .= "<".$root_element.">";
    $WENo=$_GET["last_entry_no"];
    $filter.=" where web_entry_no > $WENo and IS_SYNCHED = 0 ";
    $CompID=$_GET["comp_id"]; 
    $filter.=" and comp_id=" .$CompID;
    $Qry="SELECT web_entry_no, `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($conn->error.$Qry);
   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; 
}


    
    
?>