如何在sitemap.xml上打印或显示sitemap.php数据?

时间:2017-02-22 13:14:23

标签: php xml sitemap sitemap.xml

这是我的动态站点地图 sitemap.php 上的代码,但我想在“ sitemap.xml ”上打印或显示整个页面数据,因为谷歌始终考虑站点地图的 sitemap.xml 。此页面工作正常,但我想要 sitemap.xml 上的所有数据。 怎么做?

<?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>

1 个答案:

答案 0 :(得分:1)

    <?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);
    ?>