Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/MySQL group news/archives by year then month
    primarykey
    data
    text
    <p>I've got some code which outputs a news archive sidebar. It all works very well apart from the fact that it doesn't split the months by year properly. Rather than assigning the relevant month to its year, all months are being shown for all years! Very frustrating! Any help would be greatly appreciated.</p> <pre><code>$query = "SELECT * FROM isnews WHERE active = '1' ORDER BY YEAR(date) DESC, MONTH(date) DESC"; $resultSet = mysql_query($query); if (mysql_num_rows($resultSet)){ $newsArray = array(); echo '&lt;ul&gt;' . PHP_EOL; echo '&lt;li&gt;&lt;strong&gt;Press releases:&lt;/strong&gt;&lt;/li&gt;' . PHP_EOL; while ($newsResult = mysql_fetch_array($resultSet)){ $newDate = $newsResult['date'] ; $timePeriod = date('F Y ',strtotime($newDate)); $timePeriodY = date('Y',strtotime($timePeriod)); $timePeriodM = date('F',strtotime($timePeriod)); if (!isset($newsArray[$timePeriod])){ $newsArray[$timePeriod] = array(); } $newsArray[$timePeriod][] = $newsResult; } //by year foreach ($newsArray as $timePeriod =&gt; $newsItems){ $timePeriodY = date('Y',strtotime($timePeriod)); echo '&lt;li&gt;&lt;strong&gt;' . $timePeriodY . '&lt;/strong&gt;' . PHP_EOL; echo '&lt;ul&gt;' . PHP_EOL; //by month foreach ($newsArray as $timePeriod =&gt; $newsItems){ echo '&lt;li&gt;&lt;strong&gt;' . $timePeriod . '&lt;/strong&gt;' . PHP_EOL; echo '&lt;ul&gt;' . PHP_EOL; //news items foreach ($newsItems as $item){ echo '&lt;li&gt;'; echo '&lt;a href="'.$wwwUrl.'press-releases/'.$item["id"].'/'.$item["title"].'.php"&gt;'.$item["title"].'&lt;/a&gt;'; echo '&lt;/li&gt;' . PHP_EOL; } //end by month echo '&lt;/ul&gt;' . PHP_EOL; echo '&lt;/li&gt;' . PHP_EOL; } //end by year echo '&lt;/ul&gt;' . PHP_EOL; echo '&lt;/li&gt;' . PHP_EOL; } echo '&lt;li&gt;&amp;nbsp;&lt;/li&gt;' . PHP_EOL; echo '&lt;/ul&gt;' . PHP_EOL; } else { echo 'We currently have no press releases available'; } </code></pre> <p>Many thanks in advance S</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload