拆分站点地图将整个查询写入 PHP 中的一个文件
Split sitemaps writes whole query to one file in PHP
我编写了这个站点地图创建函数,它写入多个页面,拆分 1000 个链接。
<?php
//write sitemap file
function BuildSitemap($filename,$header,$footer,$body,$filetype,&$i){
global $config;
$ffile = $config['basedir'].'/'.$filename.$i.$ext;
$fp = @fopen($ffile, "wa+");
$filecontent = $header.'\n'.$body.'\n'.$footer;
fwrite($fp, $filecontent);
fclose($fp);
$i++;
}
function Sitemap_posts(){
global $config,$con;
$k = '';
$header = '';
$footer = '';
$body = '';
$ensm = $config['en_sm'];//enable
$burl = $config['burl'];
if($ensm == "1"){
$frequency = 'weekly';
$priority = '0.8000';
//MAKE DB QUERIES
$sql = $con->Execute("SELECT `perma`, `subtime`
FROM `posts`
WHERE `pstatus` = '1'
ORDER BY `id` DESC");
$header .= '<?xml version="1.0" encoding="utf-8"?>'."\n";
$header .= '<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="
http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
$footer .= '</urlset>';
$count = $sql->RecordCount();
$page = ceil($count/1000);
$maxlink = 1000;
for($i=0;$i < $page;$i++){
//if($i < $maxlink){
$stfrom = ($i) * $maxlink;
//} else {
//$stfrom = 0;
//}
$Query = "SELECT `perma`, `subtime`
FROM `posts`
WHERE `pstatus` = '1'
ORDER BY `id` DESC
LIMIT ".$stfrom.", ".$maxlink;
//echo $Query."<br>";
$sub = $con->Execute($Query);
$scount = $sub->RecordCount();
$results = $sub->getrows();
if($scount > 0){
//while ($row = $sub->FetchRow()){
for($j=0;$j<count($results);$j++){
$post_link = $MasterUrl.'/'.$results[$j]['perma'].'/';
$body .= '<url>
<loc>'.$post_link.'</loc>
<lastmod>'.datetimeToutc($results[$j]['subtime']).'</lastmod>
<changefreq>'.$frequency.'</changefreq>
<priority>'.$priority.'</priority>
</url>';
}
$filename = "postsitemap";
BuildSitemap($filename,$header,$footer,$body,".xml",$k);
}
}
}
}
Sitemap_posts();
我的数据库中总共有 1215 条记录,它在第一个文件中正确写入了前 1000 条记录,而在写入第二个文件时,它写入了全部 1215 条记录。根本不分裂。
当我查看循环查询时,它显示为
SELECT `perma`, `subtime` FROM `posts` WHERE `pstatus` = '1' ORDER BY `id` DESC LIMIT 0, 1000
SELECT `perma`, `subtime` FROM `posts` WHERE `pstatus` = '1' ORDER BY `id` DESC LIMIT 1000, 1000
我不知道哪里出了问题,整个晚上我都在努力度过,但没有运气。
我需要帮助...它应该拆分为 2 个文件,同时将前 1000 个链接写入第一个文件,其余 215 个链接写入第二个文件。
事情是这样的。上线
$body .= '<url>
您实际上将 $body
与变量的前一个值连接在一起 - 在本例中为前 1000 行。您应该为开始的每批新的 1000 个重新初始化 $body
。例如,您可以这样做:
if($scount > 0){
unset($body); // Start with empty body!
for($j=0;$j<count($results);$j++){
这样,每 1000 个批次都以干净的开头 $body
。
希望对您有所帮助!
--
此外,如果您接受其他建议,您可以优化用于获取总记录数的查询:
$sql = $con->Execute("SELECT `perma`, `subtime`
FROM `posts`
WHERE `pstatus` = '1'
ORDER BY `id` DESC");
...
$count = $sql->RecordCount();
这不必要地运行完整的查询,获取大量数据,甚至对其进行排序。这最终会大大降低您的代码速度,您的托管服务提供商会因此而讨厌您。相反,你可以这样:
$count = $con->Execute("SELECT COUNT(*) as count FROM `posts` WHERE `pstatus` = '1'");
$row = $count->FetchRow();
$count = $row['count']; // This is your record count
我编写了这个站点地图创建函数,它写入多个页面,拆分 1000 个链接。
<?php
//write sitemap file
function BuildSitemap($filename,$header,$footer,$body,$filetype,&$i){
global $config;
$ffile = $config['basedir'].'/'.$filename.$i.$ext;
$fp = @fopen($ffile, "wa+");
$filecontent = $header.'\n'.$body.'\n'.$footer;
fwrite($fp, $filecontent);
fclose($fp);
$i++;
}
function Sitemap_posts(){
global $config,$con;
$k = '';
$header = '';
$footer = '';
$body = '';
$ensm = $config['en_sm'];//enable
$burl = $config['burl'];
if($ensm == "1"){
$frequency = 'weekly';
$priority = '0.8000';
//MAKE DB QUERIES
$sql = $con->Execute("SELECT `perma`, `subtime`
FROM `posts`
WHERE `pstatus` = '1'
ORDER BY `id` DESC");
$header .= '<?xml version="1.0" encoding="utf-8"?>'."\n";
$header .= '<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="
http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
$footer .= '</urlset>';
$count = $sql->RecordCount();
$page = ceil($count/1000);
$maxlink = 1000;
for($i=0;$i < $page;$i++){
//if($i < $maxlink){
$stfrom = ($i) * $maxlink;
//} else {
//$stfrom = 0;
//}
$Query = "SELECT `perma`, `subtime`
FROM `posts`
WHERE `pstatus` = '1'
ORDER BY `id` DESC
LIMIT ".$stfrom.", ".$maxlink;
//echo $Query."<br>";
$sub = $con->Execute($Query);
$scount = $sub->RecordCount();
$results = $sub->getrows();
if($scount > 0){
//while ($row = $sub->FetchRow()){
for($j=0;$j<count($results);$j++){
$post_link = $MasterUrl.'/'.$results[$j]['perma'].'/';
$body .= '<url>
<loc>'.$post_link.'</loc>
<lastmod>'.datetimeToutc($results[$j]['subtime']).'</lastmod>
<changefreq>'.$frequency.'</changefreq>
<priority>'.$priority.'</priority>
</url>';
}
$filename = "postsitemap";
BuildSitemap($filename,$header,$footer,$body,".xml",$k);
}
}
}
}
Sitemap_posts();
我的数据库中总共有 1215 条记录,它在第一个文件中正确写入了前 1000 条记录,而在写入第二个文件时,它写入了全部 1215 条记录。根本不分裂。
当我查看循环查询时,它显示为
SELECT `perma`, `subtime` FROM `posts` WHERE `pstatus` = '1' ORDER BY `id` DESC LIMIT 0, 1000
SELECT `perma`, `subtime` FROM `posts` WHERE `pstatus` = '1' ORDER BY `id` DESC LIMIT 1000, 1000
我不知道哪里出了问题,整个晚上我都在努力度过,但没有运气。
我需要帮助...它应该拆分为 2 个文件,同时将前 1000 个链接写入第一个文件,其余 215 个链接写入第二个文件。
事情是这样的。上线
$body .= '<url>
您实际上将 $body
与变量的前一个值连接在一起 - 在本例中为前 1000 行。您应该为开始的每批新的 1000 个重新初始化 $body
。例如,您可以这样做:
if($scount > 0){
unset($body); // Start with empty body!
for($j=0;$j<count($results);$j++){
这样,每 1000 个批次都以干净的开头 $body
。
希望对您有所帮助!
--
此外,如果您接受其他建议,您可以优化用于获取总记录数的查询:
$sql = $con->Execute("SELECT `perma`, `subtime`
FROM `posts`
WHERE `pstatus` = '1'
ORDER BY `id` DESC");
...
$count = $sql->RecordCount();
这不必要地运行完整的查询,获取大量数据,甚至对其进行排序。这最终会大大降低您的代码速度,您的托管服务提供商会因此而讨厌您。相反,你可以这样:
$count = $con->Execute("SELECT COUNT(*) as count FROM `posts` WHERE `pstatus` = '1'");
$row = $count->FetchRow();
$count = $row['count']; // This is your record count