2

这是我的动态站点地图文件sitemap.php上的代码,但我想在“ sitemap.xml ”上打印或显示整个页面数据,因为 Google 总是考虑将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>
4

1 回答 1

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);
    ?>
于 2017-02-22T13:40:48.123 回答