我需要使用PHP xml
在xmlwriter
文件中为每个月添加文章:
$sql = "SELECT *,YEAR(FROM_UNIXTIME(timestamp)) AS YEAR,
MONTH(FROM_UNIXTIME(timestamp)) AS MONTH
FROM ".NEWS_ARTICLES." GROUP BY YEAR, MONTH ORDER BY YEAR DESC, MONTH ";
$newsdata = DataAccess::Fetch($sql);
foreach($newsdata AS $news){
$writer->openURI('./cache/xmls/posts-'.$news['MONTH'].'-'.$news['YEAR'].'.xml');
$writer->startDocument('1.0','UTF-8');
$writer->setIndent(4);
$writer->startElement('urlset');
$writer->writeAttribute('xmlns', $xmlns);
$writer->startElement('url');
$writer->writeElement('loc',$news['title']);
$writer->endElement();
$writer->endElement();
$writer->endDocument();
$writer->flush();
}
这适用于我并为每个月生成.xml
文件但是没有为每个月添加所有文章标题,我在每个月的文件中只看到one
标题!如何在xml文件中添加每个月的所有文章?
我认为我的问题是循环!!!
结果:posts-5-2015.xml
<url>
<loc>title</loc>
</url>
真的,我有5篇文章需要:
结果:posts-5-2015.xml
<url>
<loc>title</loc>
</url>
<url>
<loc>title2</loc>
</url>
<url>
<loc>title3</loc>
</url>
<url>
<loc>title4</loc>
</url>
<url>
<loc>title5</loc>
</url>
print_r($newsdata)
结果:注意:这对于大型数据库而言很简单,仅适用于架构
Array
(
[0] => Array
(
[id] => 243
[title] => test2
[story] => desc2
[timestamp] => 1442389680
[update_time] => 1442389522
[YEAR] => 2015
[MONTH] => 9
)
[1] => Array
(
[id] => 242
[title] => test1
[story] => desc
[timestamp] => 1421230680
[update_time] => 1441026399
[YEAR] => 2015
[MONTH] => 1
)
[2] => Array
(
[id] => 244
[title] => test3
[story] => desc3
[timestamp] => 1421230680
[update_time] => 1441026399
[YEAR] => 2015
[MONTH] => 5
)
[3] => Array
(
[id] => 245
[title] => test4
[story] => desc4
[timestamp] => 1421230680
[update_time] => 1441026399
[YEAR] => 2015
[MONTH] => 5
)
[4] => Array
(
[id] => 246
[title] => test5
[story] => desc5
[timestamp] => 1421230680
[update_time] => 1441026399
[YEAR] => 2015
[MONTH] => 5
)
[5] => Array
(
[id] => 247
[title] => test6
[story] => desc6
[timestamp] => 1421230680
[update_time] => 1441026399
[YEAR] => 2015
[MONTH] => 5
)
[6] => Array
(
[id] => 248
[title] => test7
[story] => desc7
[timestamp] => 1421230680
[update_time] => 1441026399
[YEAR] => 2015
[MONTH] => 5
)
)
答案 0 :(得分:0)
您的问题出在查询中:
$sql = "SELECT *,YEAR(FROM_UNIXTIME(timestamp)) AS YEAR,
MONTH(FROM_UNIXTIME(timestamp)) AS MONTH
FROM ".NEWS_ARTICLES." GROUP BY YEAR, MONTH ORDER BY YEAR DESC, MONTH ";
MySQL之外的大多数数据库都会拒绝此查询,因为您在选择多个字段时对2个字段进行分组。
group by year, month
只显示一个月的单个(随机)行,并隐藏所有其他行。
解决方案是完全删除group by
子句并重写查询,如下所示:
$sql = "SELECT *,YEAR(FROM_UNIXTIME(timestamp)) AS YEAR,
MONTH(FROM_UNIXTIME(timestamp)) AS MONTH
FROM ".NEWS_ARTICLES." ORDER BY YEAR DESC, MONTH ASC";
<强>备注强>
你没有WHERE
条款,这很奇怪。你真的想每次都选择所有文章吗?
更好的方法是通过某些过滤器限制查询中选择的文章数量
在数据库中而不是在php中过滤总是更快。
答案 1 :(得分:0)
您正在使用每个循环覆盖XML。考虑使用root并在foreach
循环之外关闭文档来启动文档:
$writer->openURI('./cache/xmls/posts-'.$news['MONTH'].'-'.$news['YEAR'].'.xml');
$writer->startDocument('1.0','UTF-8');
$writer->setIndent(4);
$writer->startElement('urlset');
foreach ($newsdata as $news){
$writer->startElement('url');
$writer->writeElement('loc',$news['title']);
$writer->endElement();
}
$writer->endElement();
$writer->endDocument();
$writer->flush();