【问题标题】:Archieve my blog'news by year and month, showing title and data of creation按年和月归档我的博客新闻,显示标题和创建数据
【发布时间】:2016-02-25 12:45:40
【问题描述】:

我正在尝试归档新闻博客。 我想让它类似于THERE

此时我可以显示年、月和每个月的发帖数,但不能显示各个帖子的标题和slug。

我的数据库结构是一个包含这些行的表: id (int)、title(varchar)、slug(varchar)、created(timestamp)。

这里是代码:

$newsdata= $db->query("
    SELECT 
         YEAR(created) AS YEAR, 
         MONTHNAME(created) AS MONTH,
         title,
         slug,
         id,
         COUNT(*) AS TOTAL 
         FROM pages 
         GROUP BY YEAR, MONTH
         ORDER BY YEAR DESC, MONTH

")->fetchALL(PDO::FETCH_ASSOC);

$currentYear = null;

foreach($newsdata AS $news){            
  if ($currentYear != $news['YEAR']){
    echo '<h2>'.$news['YEAR'].'<h2>';
    $currentYear = $news['YEAR'];
  }
    echo '<p>' .$news['MONTH']. '</p><p>' .$news['TOTAL']. '</p>';
} 

你可以看到结果HERE

我也知道有一篇有趣的帖子there 讲述了我的需求,但这并不能帮助我实现目标。

【问题讨论】:

  • 因为,这里显示:matteocorona.com/lega/m_y.php,对于不同年份的每个月,相同的标题出现 4 次。我每个月只需要看一次不同的标题。我确定查询中有问题,我不知道是什么
  • 如果您愿意,请考虑遵循以下简单的两步操作: 1. 如果您还没有这样做,请提供适当的 CREATE 和 INSERT 语句(和/或 sqlfiddle),以便我们可以更容易复制问题。 2. 如果您尚未这样做,请提供与步骤 1 中提供的信息相对应的所需结果集。
  • 我认为没有必要为此创建一个小提琴。关于我需要达到的结果,我在问的问题中添加了一个精确的例子。如果你能帮助我,请告诉我。谢谢

标签: php mysql rows archive


【解决方案1】:

如果您想阅读整个讨论,我在HERE 中找到了正确的解决方案。

代码如下:

$db= new PDO('mysql:host=62.149.150.197;dbname=Sql692231_4', 'Sql692231', 'f7157ead'); //connection setup
$stmt = $db->query("
    SELECT 
    Month(created) as Month, 
    Year(created) as Year,
    title, 
    slug, 
    created 
    FROM pages 
    ORDER BY created DESC
");

 // you will store current month here to control when the month changes
 $currentMonth = 0;
 // you will store current year here to control when the year changes
 $currentYear = 0;

 while($row = $stmt->fetch()){
     // if the year changes you have to display another year
    if($row['Year'] != $currentYear) {
        // reinitialize current month
        $currentMonth = 0;
        // display the current year
        echo "<li class=\"cl-year\">{$row['Year']}</li>";
        // change the current year
        $currentYear = $row['Year'];
    }
    // if the month changes you have to display another month
    if($row['Month'] != $currentMonth) {
        // display the current month
        $monthName = date("F", mktime(0, 0, 0, $row['Month'], 10));
        echo "<li class=\"cl-month\">$monthName</li>";
        // change the current month
        $currentMonth = $row['Month'];
    }
    // display posts within the current month
    //$slug = 'a-'.$row['Month'].'-'.$row['Year'];
    //echo "<li class=\"cl-posts\"><a href='$slug'>$monthName</a></li>";
    echo '<li class="cl-posts active"><a       href="http://www.matteocorona.com/cms_images/page.php?page='.$row['slug'].'">'.$row['title'].'</a></li>';
    echo '<li class="cl-posts active">'.$row['created'].'</li>';
    echo "<br/>";
}

结果,HERE

【讨论】:

  • 太棒了!谢谢
猜你喜欢
  • 2012-07-31
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 2011-01-29
  • 2016-11-16
  • 1970-01-01
相关资源
最近更新 更多