<?php require('includes/cmain.inc.php'); 

$query = "SELECT tc.country_name,tc.country_url,tc.country_id,tc.updated_on,tcy.state_id,ts.state_name,tcy.county_name,tcy.county_short_name 
           FROM tbl_country tc JOIN tbl_county tcy on tc.country_status=tcy.country_id 
		   JOIN tbl_state ts ON tcy.state_id=ts.state_id 
		   WHERE tc.parent_childs!='0' ORDER BY ts.state_name ASC LIMIT 50000,49000";
//print_r($query);exit();
$result = db_query($query);


//$resultstate=mysqli_query($conn,$result);
$countstate=mysqli_num_rows($result);


header("Content-Type: application/xml; charset=utf-8");

echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 

echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . PHP_EOL;
?>

<?php
while($row = mysqli_fetch_array($result))
{
  $link2= strtolower(str_replace(" ","-",$row['country_url']));
  $link3= strtolower(str_replace(" ","-",$row['state_name']));
  $link4= strtolower(str_replace(" ","-",$row['county_name']));
 $testurl=SITE_WS_PATH."/".$link2."/".$link3."/".$link4;
 echo '<url>' . PHP_EOL;
 echo '<loc>'. $testurl .'</loc>' . PHP_EOL;
/* Convert the date format to W3C Datetime format in UTC*/

$query2 = "SELECT tc.country_id,tc.country_name,tsy.child_name,tsy.state,tsy.state_name,tsy.county,tsy.county_name,tsy.updated_on 
           FROM `tbl_statecounty` tsy 
           JOIN tbl_country tc ON tsy.child=tc.country_id 
           JOIN tbl_state ts ON ts.state_id=tsy.state 
           JOIN tbl_county tcy ON tcy.county_id=tsy.county 
           WHERE tsy.state_name='".$row['state_name']."' and tsy.child_name='".$row['country_url']."' and tsy.county_name='".$row['county_name']."' and county!='0'";
$result2 = db_query($query2);
$row2 = mysqli_fetch_array($result2);
$count2=mysqli_num_rows($result2);
$up=$row2['updated_on'] ?? '';
if($count2!='0'){
$w3cDatetime = gmdate('c', strtotime($up));
echo '<lastmod>' . $w3cDatetime . '</lastmod>' . PHP_EOL;
}else{

$w3cDatetime = gmdate('c', strtotime($row["updated_on"]));
echo '<lastmod>' . $w3cDatetime . '</lastmod>' . PHP_EOL;
}
 echo '<priority>0.80</priority>' . PHP_EOL;
 echo '</url>' . PHP_EOL;
}

echo '</urlset>' . PHP_EOL;

?>