Jump to content

[SOLVED] How to: Sort records into same month by timestamp?


Hilly_2004

Recommended Posts

Hi guys,

 

Bit of help. I have a table in a database called tbl_news.

 

How would I go about sorting all of the records in that table into the same month by the timestamps at which they were created....

 

e.g.

 

Basically I want the page to dynamically check if records were posted in a certain month of the year and if they were echo a link......

 

June 2008

May 2008

April 2008

 

Then that link would go to a page where all the news stories posted in that month would be displayed.

What exactly when do you want to sort them?

 

When exactly do you want to sort them?

 

Do you want to sort them when someone requests only post from May 2008 or sort them and write them to a text document or ?

 

 

What I have in mind is for example a blog or news article archive where you could click "April 2008" and see all posts from that month... is that what you wanna do?

Spot on Wuhtzu...

 

In my mind I see 2 pages.

 

1st page is a script which gets all the records in tbl_news and checks to see which month and it's year a record has been added

 

e.g. Has there been anything posted in April 2008? No....don't display a link. Has there been anything posted in May 2008? Yes........display "May 2008" and a link to the next page.

 

The next page then displays all of the records added in May 2008 which might be 4 or 5.

 

Hope that makes sense?

SELECT * FROM `database` WHERE `date` LIKE '200805%' ORDER BY `date` Assuming you're using a mysql timestamp

The problem with that I'm guessing is that it requires me to enter the specific month e.g. May 2008.

 

I want the script to dynamically check if there have been any records created, and sort them into the relevant months....

 

e.g. The end list may look like:

 

June 2007

January 2008

April 2008

May 2008 etc....

$result = mysql_query("SELECT DISTINCT DATE_FORMAT(Date, '%M %Y') as monthyear FROM tbl_news ORDER BY Date",$connect);

 

while($myrow = mysql_fetch_array($result))

{

 

echo $monthyear;

echo "</br>";

 

}

 

Obviously I'm doing it wrong.

Works a treat....cheers Barand. One more, and I think this is the easy part.

 

Obviously with this it will echo out:

 

July 2008 etc...

 

On the next page I have to pass across a variable for the SELECT procedure to get all of the records in that month.

 

Will it be something like:

 

SELECT * FROM tbl_news WHERE Date =  '%M %Y' ORDER BY Date

You will need to select two date formats

 

- the one to be displayed (which you have now)

- the one for the link

 

<?php
$sql = "SELECT DISTINCT 
		DATE_FORMAT(Date, '%M %Y') as monthyear, 
		DATE_FORMAT(Date,'%Y-%m') as link
		FROM tbl_news ORDER BY Date";
$result = mysql_query($sql);

while($myrow = mysql_fetch_array($result))
{
echo "<a href='showItems.php?month={$myrow['link']}'>$monthyear</a></br>";
}
?>

 

On the linked page (showItems.php) you beed something like

 

<?php
$search = $_GET['month'];
$sql = "SELECT * FROM tbl_news WHERE Date LIKE '$search%' ORDER BY Date"

// process query
?>

 

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.