我有一个网站,我定期添加一个页面的日志。目前,我正在遍历我的数据库以获取几百个URL,然后将它们添加到sitemap-1中,然后获取下一个几百个URL并将它们添加到sitemap-2中。这一切都是我亲手完成的。
我想使用while循环来创建所有新页面,并将它们插入到数据库中。它应该检查我当前的站点地图-x,以确定是否达到了URL计数阈值,然后创建一个新的sitemap++并开始添加页面,或者只是添加到当前站点地图。
我如何才能做到这一点呢?
我有一些代码来创建网站地图,但我不确定如何检查它是否与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');
?>发布于 2013-05-21 06:31:36
$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,并进行适当的修复以使其正常工作,但您得到了重点。
https://stackoverflow.com/questions/16658884
复制相似问题