Page 1 of 1

How to archive data by month?

Posted: Sun Nov 14, 2010 8:16 pm
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.

Re: How to archive data by month?

Posted: Sun Nov 14, 2010 8:20 pm
by Celauran

Code: Select all

SELECT * FROM database_name WHERE dates LIKE '2010-11%'

Re: How to archive data by month?

Posted: Sun Nov 14, 2010 8:22 pm
by rolyestemonio
What if i would like to display all months available in my database?

Re: How to archive data by month?

Posted: Sun Nov 14, 2010 8:30 pm
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.

Re: How to archive data by month?

Posted: Sun Nov 14, 2010 8:56 pm
by rolyestemonio
Thanks. Your the best. :P

Re: How to archive data by month?

Posted: Sun Nov 14, 2010 9:07 pm
by rolyestemonio
Thanks you very much. I already solve it. I really appreciate your helps. Happy Merry X-mass.