How to archive data by month?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rolyestemonio
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 10:30 pm
Location: Metro Manila - Paranaque City
Contact:

How to archive data by month?

Post by rolyestemonio »

Hi Guys,

Can I ask another question? I am really a beginner in PHP.
I have a problem regarding of archiving. How we are going to archives data by month?
I have 4 fields(id, dates, title,description). I want to archives my data by month in which you can see the Month automatically from database MySQL.

Example

Month retrieved from database

News for November
News for October
News for September

when you click each of this month it will directly open a page archives for each month.

Can anyone help me please. I really confuse how to do it. Thanks for advance guys.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to archive data by month?

Post by Celauran »

Code: Select all

SELECT * FROM database_name WHERE dates LIKE '2010-11%'
rolyestemonio
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 10:30 pm
Location: Metro Manila - Paranaque City
Contact:

Re: How to archive data by month?

Post by rolyestemonio »

What if i would like to display all months available in my database?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to archive data by month?

Post by Celauran »

Maybe something like this?

Code: Select all

$sql = "SELECT dates FROM database ORDER BY dates DESC";
$res = mysql_query($sql);

$current_month = 0;

while ($row = mysql_fetch_array($res))
{
    $cm = substr($row['dates'], 5, 2);
    if ($cm != $current_month)
    {
        $current_month = $cm;
        echo "<a href=\"somepage.php\">News for " . substr($row['dates'], 0, 7) . "</a><br />";
    }
}
EDIT: Read your original post wrong. Stick the above loop in a form and pass the value of substr($row['dates'], 0, 7) to the page that will display the news.
rolyestemonio
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 10:30 pm
Location: Metro Manila - Paranaque City
Contact:

Re: How to archive data by month?

Post by rolyestemonio »

Thanks. Your the best. :P
Last edited by rolyestemonio on Sun Nov 14, 2010 9:08 pm, edited 1 time in total.
rolyestemonio
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 10:30 pm
Location: Metro Manila - Paranaque City
Contact:

Re: How to archive data by month?

Post by rolyestemonio »

Thanks you very much. I already solve it. I really appreciate your helps. Happy Merry X-mass.
Post Reply