Jump to content

Recommended Posts

Hello

 

I am after some guidance on the following scenario:

 

I have a news page that pulls from a database and would like to seperate it out by year, see below

 

2010

 

Bulletin 2 - 23/11/2010 - Some news here.....

Bulletin 1 - 21/11/2010 - Some news here.....

 

 

 

2009

 

Bulletin 2 - 23/11/2009 - Some news here.....

Bulletin 1 - 21/11/2009 - Some news here.....

 

 

 

How can i do this automatically and seperate them out by year like above

Link to comment
https://forums.phpfreaks.com/topic/219555-php-for-each-groups/
Share on other sites

<?php
$news = array();
$result = mysql_query("SELECT id, title, date, YEAR(date) AS year, SUBSTRING(body,0,100) AS snippet FROM news ORDER BY date DESC");
while($row = mysql_fetch_assoc($result)) {
$news[$row['year']][] = $row;
}

/*
display news
*/
foreach($news as $year => $articles) {
print "<h1>".$year."</h1>";
foreach($articles as $article) {
	print "<p>".$article['title']." - ".date("d/m/Y", strtotime($article['date']))." - ".$article['snippet']."</p>";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/219555-php-for-each-groups/#findComment-1138322
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.