下面的代码是我sitemap.xml
文件的一部分。我的目标是写出我批准文章的最后修改日期。该数据有两种可能性。
- 如果文章没有被批准的评论,那么
lastmod
就是文章的批准日期。 - 如果文章至少有 1 条批准评论,
lastmod
则为最后批准评论的批准日期。
我的输出中的错误是:
- 我的数据库中有一些没有评论的文章,但这些无评论的文章获得了其他一些文章的最后评论的批准日期。结果,由于我今天有一些评论被批准,所有那些没有评论的文章的最后模式都是今天的日期。但是这些文章已经很老了。
$newsql
在我的第二个while循环中在屏幕上打印“newstmt prepare error”,所以if ($newstmt = $connection->prepare($newsql))
部分不起作用
你能纠正我吗?
谢谢您最好的问候
代码
<?php
//if spesific article is approved then its last modification date = approval date of article
//if spesific article has approved comment(s), then modification date = approval date of its last comment
$sql = "SELECT col_author, col_title, col_approvaldate FROM articles WHERE col_status = ? ORDER by col_approvaldate DESC";
if ($stmt = $connection->prepare($sql))
{
/* bind parameters */
$stmt -> bind_param("s", $bindparam1);
/* assign value */
$bindparam1 = 'approved';
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($author, $title, $articledate);
/* fetch values */
while ($stmt->fetch())
{
//if exist, get approved newest comment approval date
$newsql = "SELECT col_approvaldate FROM comments WHERE col_status = ? AND col_for_author = ? AND col_for_title = ? ORDER by col_approvaldate DESC LIMIT 1";
if ($newstmt = $connection->prepare($newsql))
{
/* bind parameters */
$newstmt -> bind_param("sss", $ybindparam1, $ybindparam2, $ybindparam3);
/* give values */
$ybindparam1 = 'approved';
$ybindparam2 = $author;
$ybindparam3 = $title;
/* execute statement */
$newstmt->execute();
/* bind result variables */
$newstmt->bind_result($commentdate);
/* fetch values */
$biggerdate = '';
while ($newstmt->fetch())
{
// is there any approved comment for this article?
if (!is_null($commentdate))
{$biggerdate = $commentdate;}
}
/* close statement */
$newstmt->close();
}
else {echo 'newstmt prepare error';}
//print the result
echo '<url>'."\r\n";
echo "\t".'<loc>'.root_folder.urlencode('articles').'/'.urlencode(space_to_dash($author)).'/'.urlencode(space_to_dash(no_punctuation($title))).'</loc>'."\r\n";
//if there is no approved comment for this article
if ($biggerdate == '')
{
$biggerdate = $articledate;
}
$datetime = new DateTime($biggerdate);
$biggerdate = $datetime->format('Y-m-d\TH:i:sP');
echo "\t".'<lastmod>'.$biggerdate.'</lastmod>'."\r\n";
echo '</url>'."\r\n";
}
/* close statement */
$stmt->close();
}
?>