<?php
ob_start();
include("connection.php");

// Set content type as XML
header("Content-Type: application/xml; charset=utf-8");
// Fetch dynamic URLs from the database
$query = "SELECT * FROM posts";
$result = $con->query($query);

// Clear any previous output
ob_clean();

// Start XML output
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n";

// Add homepage
echo "    <url>\n";
echo "        <loc>https://couponclap.com/</loc>\n";
echo "        <lastmod>" . date('Y-m-d') . "</lastmod>\n";
echo "        <changefreq>daily</changefreq>\n";
echo "        <priority>1.0</priority>\n";
echo "    </url>\n";

// Add static URLs
$static_urls = [
    "https://couponclap.com/",
    "https://couponclap.com/home",
    "https://couponclap.com/category-deals",
    "https://couponclap.com/todays-top-deals",
    "https://couponclap.com/top-coupons",
    "https://couponclap.com/best-cashback-stores",
    "https://couponclap.com/all-stores",
    "https://couponclap.com/latest-coupons",
    "https://couponclap.com/seo/about-us",
    "https://couponclap.com/contact-us",
    "https://couponclap.com/seo/privacy-policy",
    "https://couponclap.com/seo/terms-conditions",
    "https://couponclap.com/seo/disclaimer",
    "https://couponclap.com/seo/faq"
];

foreach ($static_urls as $url) {
    echo "    <url>\n";
    echo "        <loc>{$url}</loc>\n";
    echo "        <lastmod>" . date('Y-m-d') . "</lastmod>\n";
    echo "        <changefreq>weekly</changefreq>\n";
    echo "        <priority>0.8</priority>\n";
    echo "    </url>\n";
}

// Loop through database results and generate URLs
while ($row = $result->fetch_assoc()) {
    $game_name = urlencode(strtolower(str_replace(' ','-', $row['slug'])));
   
    echo "    <url>\n";
   echo "<loc>https://couponclap.com/deals/" 
    . htmlspecialchars($game_name, ENT_QUOTES, 'UTF-8') 
    . "</loc>\n";
    echo "        <changefreq>weekly</changefreq>\n";
    echo "        <priority>0.8</priority>\n";
    echo "    </url>\n";
}

// Close XML structure
echo "</urlset>";

$con->close();
?>
