Can anyone help me out? How to archive by month and year?
Hi guys can anyone help me out how to archive by month and year?
and this data came from my database MySql. And if you click that date it will display the content corresponds to that. Example : News for 2010 November 10, 2010 October 10, 2010 News for 2009 November 10, 2009 October 10, 2009
4 Answers
How to display data using this method?
<?php include("db.php"); $result = mysql_query("SELECT YEAR(local_date) AS year ,MONTH(local_date) AS month ,count(ID) AS posts FROM tbl_localnews GROUP BY YEAR(local_date), MONTH(local_date) ORDER BY local_date DESC"); if(mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { print("<br><a href='news.php?year=".$row['year']."&month=".$row['month']."'>News for ".$row['month'].",".$row['year']."</a>"); } } ?> Posted: roly 0 of 0 people found this answer helpful. Did you? Yes No
What is the wrong with this guys? Why is it that when i view the month of November 2010 it also view the month of 2009?Can anyone help me?
<?php include("db.php"); $query = mysql_query("SELECT distinct YEAR(local_date) as year FROM tbl_localnews order by local_date desc"); while($row = mysql_fetch_array($query)) { //$row = array_unique($r); $unique_year = $row['year']; echo($unique_year)."<br>"; $query2 = mysql_query("select distinct monthname(local_date) as month from tbl_localnews where local_date like '$unique_year%' order by local_date desc"); while($r2 = mysql_fetch_array($query2)) { //$row2 = array_unique($r2); $unique_month = $r2['month']; print("<a href='news.php?month=$unique_month'&year=$unique_year>News for $unique_month</a><br>"); //echo($unique_month); } } ?> 0 of 1 people found this answer helpful. Did you? Yes No use my method will show you the year and each month has posts
try this method
SELECT YEAR( post_date ) AS `year` , MONTH( post_date ) AS `month` , count( ID ) AS posts FROM posts_table GROUP BY YEAR( post_date ) , MONTH( post_date ) ORDER BY post_date DESCthis will show in example: year | month | posts 2010 | 8 | 15 2010 | 9 | 11this is the wordpress method to show the full archive but if you want a simple method like show all the posts under each month like: 2009
2008
$rows = $db->query(\'select year, title from news\'); $smarty->assign(\'news\', $rows); {if count($rows) > 0} {assign var=lastYear value=0} <ul><li> {foreach name=foo from=$rows item=item} {if $smarty.foreach.foo.first} {$row.year}<ul> {elseif $lastYear != $row.year} </ul></li><li>{$row.year}<ul> {/if} {assign var=lastYear value=$row.year} <li>{$row.title}</li> {/foreach} </ul></li></ul> {/if}The other one is to group the values in php: $rows = $db->query(\'select year, title from news\'); $news = array(); foreach($rows as $row) { $news[$row[\'year\']][] = $row[\'title\']; } $smarty->assign(\'news\', $news); <ul> {foreach from=news key=year item=rows} <li>{$year}<ul> {foreach from=$rows item=row} <li>{$row}</li> {/foreach} </ul></li> {/foreach} </ul> Posted: MacOS 1 of 1 people found this answer helpful. Did you? Yes No I don't get it. :( do you want a full year/months archive like the wordpress one?
what about
GROUP BY YEAR(record_date), MONTH(record_date)Check out the date and time functions in MySQL. Posted: greentree 2 of 2 people found this answer helpful. Did you? Yes No this is the right method for that |
© Advanced Web Core. All rights reserved