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 ?
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