按年份显示存档博客文章

时间:2019-02-28 16:56:49

标签: php mysql

我试图按年份显示博客文章,但是我无法显示博客文章,我是否需要重新循环?

我想要一个归档部分,以下拉方式返回该年份下的博客文章-您可以看到示例here

<?php

$monthText = "";
$link_archive="";
$i=1;

$query1="SELECT DISTINCT YEAR(`date`) AS `Year` FROM posts WHERE type='news' GROUP BY `Year` ORDER BY `date` DESC";
$sql1 = $dbh->prepare($query1);
$sql1->execute();

while ($row1 = $sql1->fetch( PDO::FETCH_OBJ )) {
    $inner_posts="";

    $dateObj   = DateTime::createFromFormat('!m', $row1->Month);
    $year = $row1->Year;

    $query2="SELECT * FROM posts WHERE type='news' AND YEAR(`date`) = ? ORDER BY `date` DESC";
    $sql2 = $dbh->prepare($query2);
    $sql2->execute([$year]);
while ($row2 = $sql2->fetch( PDO::FETCH_OBJ )) {
    $inner_posts.="<div class='post'>
                <h2><a href='/{$GLOBALS['post_url']}/{$row->url}'>$row->title</a></h2>
                $blog_post_sh
              </div>";
}

if ($inner_posts != '') {
    $inner_posts = "<ul id='list{$i}' class='inner_posts'>{$inner_posts}</ul>";
}



$monthText.="<li>
<a class='open{$i}' href='/{$GLOBALS['posts_url']}$link_archive'>
{$year}
</a>
{$inner_posts}
</li>";
$i++;

}


?>

<ul>
    <?php echo $monthText; ?>
</ul>

0 个答案:

没有答案