0

我有一个网站,我会定期向其中添加页面日志。目前我正在遍历我的数据库以获取几百个 URL,然后将它们添加到 sitemap-1,然后获取接下来的几百个并将它们添加到 sitemap-2。我都是手工完成的。

我想使用一个while循环来创建所有新页面并将它们插入数据库。它应该检查我当前的站点地图-x 以确定是否达到 URL 计数阈值,然后创建一个新的站点地图++并开始添加页面或只是添加到当前站点地图。

我怎样才能做到这一点?

我有一些代码来创建站点地图,但我不确定如何检查它是否已满 2000 个 url,然后继续创建一个新的。

<?php


/* create a dom document with encoding utf8 */
$domtree = new DOMDocument('1.0', 'UTF-8');

/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("urlset");
$xmlRoot -> appendChild(new DomAttr('xmlns',  'http://www.sitemaps.org/schemas/sitemap/0.9'));
$xmlRoot -> appendChild(new DomAttr('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'));
$xmlRoot -> appendChild(new DomAttr('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'));

/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);

/* tehloop */
while ($row = mysql_fetch_array($result)){
    $currentTrack = $domtree->createElement("url");
    $currentTrack = $xmlRoot->appendChild($currentTrack);
    $currentTrack->appendChild($domtree->createElement('loc','website'));
    $currentTrack->appendChild($domtree->createElement('changefreq','weekly'));
    $currentTrack->appendChild($domtree->createElement('priority','1.0'));
}

/* save the xml sitemap */
$domtree->formatOutput = true;
$domtree->preserveWhitespace = false;
$domtree->save('sitemap.xml'); 

?>
4

1 回答 1

0
$tracks = array();
$count = 0;

while ($row = mysql_fetch_array($result)){
    if($count >= 2000){
        $count = 0;
        array_push($currentTrack);
        unset($currentTrack);
        //Reset your domtree object here too...
    }
    $currentTrack = $domtree->createElement("url");
    $currentTrack = $xmlRoot->appendChild($currentTrack);
    $currentTrack->appendChild($domtree->createElement('loc','website'));
    $currentTrack->appendChild($domtree->createElement('changefreq','weekly'));
    $currentTrack->appendChild($domtree->createElement('priority','1.0'));
}

//Now for each array index loop through file creation 
for($i = 0; $i < $tracks.length; $i++){
    /* save the xml sitemap */
    $domtree->formatOutput = true;
    $domtree->preserveWhitespace = false;
    $domtree->save('sitemap' . $i . '.xml'); 
}

您必须在此处添加代码以重置您的 domtree 并进行适当的修复以使其正常工作,但您明白了。

于 2013-05-20T22:31:35.673 回答