Forums

PHP

This topic is locked

News Archive table using PHP and MySQL

Posted 21 Mar 2010 03:03:18
1
has voted
21 Mar 2010 03:03:18 Vernon Large posted:
Hi,

I need a little advice and hope someone can help!

I am creating a page to enable the visitor to look at older news items and I want to create a table that shows that resembles this

Year JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
2005 X X X X X
2007 X X X X X X X
2008 X X X X X X
2009 X X
2010 X X

My database table (news_table) has the following fields

id
newsDate (Format YYYY-MM-DD)
newstitle
newsInfo
photo
caption
update

In the table, I only want years to show if there is actually a news item in the DB table corresponding to that year and also only an X is shown under the month if a record for that year/month is available. If an X is shown, I want it to like to a page that shows all the records for that year/month!

Any pointers would be helpful

Cheers

Phil

Replies

Replied 30 Mar 2010 15:31:39
30 Mar 2010 15:31:39 Roddy Dairion replied:
I don't know if you've fixed this or not but i have this for you if it helps (FYI i haven't tested any of it, if there's any errors you can't debug just reply back i will try and fix it)

<table border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td>News</td>
		<td>Jan</td>
		<td>Feb</td>
		<td>Mar</td>
		<td>Apr</td>
		<td>May</td>
		<td>June</td>
		<td>July</td>
		<td>Aug</td>
		<td>Sep</td>
		<td>Oct</td>
		<td>Nov</td>
		<td>Dec</td>
	</tr>
<?php
$query="select date_format(newsDate,'%Y') as `News_Year`,newsDate from news_table group by `News_Year`";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
?>
	<tr>
		<td><?php echo $row['News_Year'];?></td>
		
<?php
	for($i=0;$i < 12;$i++)
	{
		$date=explode('-',date("Y-01-d"));
		$checkdate=date("Y-m",mktime(0,0,0,$date[1]+$i,$date[2],$date[0]));
		$query2="select date_format(newsDate,'%Y-%m') as `News_Year` from news_table where `News_Year` = '".$checkdate."'";
		$result2=mysql_query($query2);
		if(mysql_num_rows($result2) > 0)
		{
?>
			<td>X</td>
<?php
		}else{
?>
			<td>&nbsp;</td>
<?php
		}
	}	
?>
	</tr>
<?php
}
?>
</table>



Edited by - Roddy Dairion on 30 Mar 2010  15:33:13

Reply to this topic