PHP mysql-生成不超过50k行限制的站点地图
我有下面的PHP/MYSQL站点地图,可以在浏览器中生成站点地图。如何使其保存到文件并在50000上拆分?我无法让它正常工作,因为脚本只是输出为纯文本。我希望脚本输出到一个文件中 目前PHP mysql-生成不超过50k行限制的站点地图,php,xml,Php,Xml,我有下面的PHP/MYSQL站点地图,可以在浏览器中生成站点地图。如何使其保存到文件并在50000上拆分?我无法让它正常工作,因为脚本只是输出为纯文本。我希望脚本输出到一个文件中 目前 欢迎来到StackOverflow 只需在foreach中运行一个计数器,并将其分解为所需的值 下面是未测试的代码 if($result !== false) { // Parse the result set, and add the URL in the XML structure
欢迎来到StackOverflow
只需在foreach中运行一个计数器,并将其分解为所需的值
下面是未测试的代码
if($result !== false) {
// Parse the result set, and add the URL in the XML structure
$counter = 0;
foreach($result as $row) {
$counter++
$xmlsitemap .= '
<url>
<loc>'. $row['shortUrl'] .'</loc>
<priority>0.5</priority>
<changefreq>weekly</changefreq>
</url>';
if($counter==4900){
break;
}
}
}
if($result!==false){
//解析结果集,并在XML结构中添加URL
$counter=0;
foreach($结果为$行){
美元柜台++
$xmlsitemap.='
“.$row['shortUrl']”
0.5
每周的
';
如果($counter==4900){
打破
}
}
}
让我们看看生成url记录的代码
<url>
<loc>'. $row['shortUrl'] .'</loc>
<priority>0.5</priority>
<changefreq>weekly</changefreq>
</url>';
“.$row['shortUrl']”
0.5
每周的
';
如上所述,一条记录包含5行。所以你的数组必须最多包含10.000条记录,对吗
如果我们将数组拆分为最多包含10.000条记录的块,我们可以轻松地将每个块记录保存在不同的文件中
try {
// connect db and get records
# give attention here
$chunks = array_chunk($result, 10000)
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
// Parse the result set, and add the URL in the XML structure
foreach ($chunks as $key => $chunk) {
$xmlsitemap .= '</urlset $namespaces>';
foreach ($chunk as $row){
$xmlsitemap .= '
<url>
<loc>' . $row['shortUrl'] . '</loc>
<priority>0.5</priority>
<changefreq>weekly</changefreq>
</url>';
}
$xmlsitemap .= '</urlset>';
file_put_contents("path/to/directory/sitemap-".$key.".xml", $xmlsitemap);
}
}
}
// check errors
试试看{
//连接数据库并获取记录
#注意这里
$chunks=array\u chunk($result,10000)
//如果SQL查询成功执行($result not false)
如果($result!==false){
//解析结果集,并在XML结构中添加URL
foreach($key=>$chunk的块){
$xmlsitemap.='';
foreach($chunk作为$row){
$xmlsitemap.='
“.$row['shortUrl']”
0.5
每周的
';
}
$xmlsitemap.='';
文件内容(“path/to/directory/sitemap-”$key.“.xml”,$xmlsitemap);
}
}
}
//检查错误
顺便说一句,不要忘记为那些@Shadow创建站点地图索引。我认为不应该删除
pdo
标记。@Variable没有一个答案使用任何pdo函数或对象。