Php: Archives system like blog

匆匆过客 提交于 2019-12-02 17:01:06

问题


On my website, I have a "news" page where the customer can add an unlimited number of news (dates are stored in the DB ( with CURRENT_TIMESTAMP )).

For example when the user clicks on July, only news of July will be appear on the page.

For now, I only have this :

<?php
    $q = $db->query("SELECT dateNews FROM news GROUP BY dateNews");

    while($data = $q->fetch()){
        $dateMonth= $data['dateNews'];
        echo "<a href='#''><p>".strftime('%B %Y', strtotime($dateMonth))."</span></p></a>";
    }                   
?>

But I want this :

My dates are listed correctly, but I do not really know how to solve the problem.

Can someone help me please ?

EDIT :

After much research, I found a solution for merge the same months :

<?php                   
    $q = $db->query("SELECT YEAR(dateNews) AS YEAR, MONTH(dateNews) AS MONTH, COUNT(*) AS newsCount FROM news GROUP BY YEAR, MONTH");

    while($data = $q->fetch()){
        $dateMonth = $data['MONTH'];
        echo "<a href='#''><p>".utf8_encode(strftime('%B %Y', strtotime($dateMonth)))." (" . $data['newsCount'] . ")</span></p></a>";
    }
?>

Months are correctly merged, but now I have another problem that I can't resolve :

I really don't know what to do or how to do. Somebody knows something ?


回答1:


Assuming that dateNews is the month (which it appears to be), then you've almost got it. You just need to also select the count of each grouping and then display it.

<?php
    $q = $db->query("SELECT dateNews, COUNT(*) AS newsCount FROM news GROUP BY dateNews");

    while($data = $q->fetch()){
        $dateMonth= $data['dateNews'];
        echo "<a href='#''><p>".strftime('%B %Y', strtotime($dateMonth))." (" . $data['newsCount'] . ")</span></p></a>";
    }                   
?>


来源:https://stackoverflow.com/questions/24963143/php-archives-system-like-blog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!