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

// Kết nối cơ sở dữ liệu của bạn
$host = 'localhost';
$db   = 'u157545268_tailieuquy';
$user = 'u157545268_tailieuquy';
$pass = 'Hatinhhatinh12**';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
} catch (PDOException $e) {
    die("Lỗi kết nối database: " . $e->getMessage());
}

$domain = 'https://tailieuquy.com'; // Thay bằng tên miền của bạn

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://sitemaps.org">
    <!-- Trang chủ -->
    <url>
        <loc><?php echo $domain; ?>/</loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <?php
    // Lấy dữ liệu bài viết/sản phẩm từ Database
    $stmt = $pdo->query("SELECT slug, updated_at FROM posts WHERE status = 1 ORDER BY updated_at DESC");
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $loc = $domain.'/'.htmlspecialchars($row['uid'].'-'.htmlspecialchars($row['url_url']);
        $lastmod = date('c', strtotime($row['updated_at']));
    ?>
    <url>
        <loc><?php echo $loc; ?></loc>
        <lastmod><?php echo $lastmod; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php } ?>
</urlset>