Jump to content

PHP for each groups?


mds1256

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.