Amar Prem
Amar Prem

Reputation: 289

How to print or show sitemap.php data on sitemap.xml?

This is my code on file sitemap.php for my dynamic sitemap but I want to print or show this whole page data on "sitemap.xml" because of Google always consider sitemap.xml for sitemap.This page is working fine but I want all data on sitemap.xml. How to do this?

<?php 
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
include 'includes/connectdb.php';
?>

<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">

    <url>
        <loc>http://www.mobilesapp.com/</loc>
        <priority>1.00</priority>
    </url>

<?php
   $stmt = $con->prepare("SELECT url,modified FROM localnews");
   $stmt->execute();
   $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
   foreach($rows as $row){
?>
<url>
    <loc>http://www.mobilesapp.com/<?= $row['url'] ;?></loc>
    <lastmod><?= $row['modified']; ?></lastmod>
    <changefreq>always</changefreq>
    <priority>1</priority>
</url>
<?php } ?>

<?php
   $stmt = $con->prepare("SELECT url,modified FROM socialnews");
   $stmt->execute();
   $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
   foreach($rows as $row){
?>
<url>
    <loc>http://www.mobilesapp.com/<?= $row['url'] ;?></loc>
    <lastmod><?= $row['modified']; ?></lastmod>
    <changefreq>always</changefreq>
    <priority>1</priority>
</url>
<?php } ?>

</urlset>

Upvotes: 2

Views: 1554

Answers (1)

user7601056
user7601056

Reputation:

    <?php 
    $xml = '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">';
    // mysql select
    foreach ($rows as $row) {
        // add next part to xml
        $xml .= '
        <url>
            <loc>http://www.mobilesapp.com/'.$row['url'].'</loc>
            <lastmod>'.$row['modified'].'</lastmod>
            <changefreq>always</changefreq>
            <priority>1</priority>
        </url>
        ';
    }
    $xml .= '</urlset>';
    // show 
    echo $xml;
    // and save to file 
    file_put_contents('sitemap.xml', $xml);
    ?>

Upvotes: 1

Related Questions