我有一个网站,我定期添加页面日志。目前,我正在遍历我的数据库以获取几百个URL,然后将它们添加到sitemap-1然后获得接下来的几百个并将它们添加到sitemap-2。我是手工完成的。
我想使用while循环创建所有新页面并将它们插入到数据库中。它应该检查我当前的sitemap-x以确定是否达到URL计数阈值,然后创建一个新的站点地图++并开始添加页面或只是添加到当前站点地图。
我该如何做到这一点?
我有一些代码来创建站点地图,但我不知道如何检查它是否已满2000个网址,然后继续创建一个新的。
<?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');
?>
答案 0 :(得分: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并执行相应的修复以使其正常运行,但您明白了这一点。