list last ten months (blog archieves like)

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
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

list last ten months (blog archieves like)

Post by fael097 »

hi, im making a simple blgo system, and i'd like to show a list of the last ten months, so the user can click the month name, and it will open the blog page with all the posts from that month, just like we usually see in blogs.
any ideas?
thanks in advance
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: list last ten months (blog archieves like)

Post by mecha_godzilla »

This should be fairly straightforward:

1. Work out what month we're on:

Code: Select all

$today = getdate();
$day = $today['mday'];
$month = $today['mon'];
$year = $today['year'];
Create a variable called something like $months_menu as follows:

Code: Select all

$months_menu = '<select name="month">';
Then use a for() loop to generate the options for the <select> element. This will be a little bit more complicated because you have to count down from whatever month you're on (month 4 for April) and then change the year to 2010 and continue counting down from month 12 (December).

We know that you need to display 10 months so a good starting point would be:

Code: Select all

for ($i = 0; $i < 10; $i++) {

    $month = str_pad((int) $month,2,"0",STR_PAD_LEFT); // pad the month so it is formatted as two digits

    if ($month != 0) {
        $months_menu .= '<option value="' . $year . '_' . $month . '">' . $month . '/' . $year . '</option>';
        $month--;
    } else {
        $month = 12;
        $year--;
        $months_menu .= '<option value="' . $year . '_' . $month . '">' . $month . '/' . $year . '</option>';
        $month--;
    }
    
}
Then generate the last part of the <select> element and echo() it out:

Code: Select all

$months_menu .= '</select>';

echo $months_menu;
What you'll need to do to complete your script is generate the month names in the dropdown menu and prepare the menu values so that they can be inserted into a DB query, remembering that date format is usually '2011-04-03' but your set-up might be different. Your query will also need to retrieve blogs dated between the start and end of the month so you might want to calculate what the last valid date of each month as well.

An example query for March 2011 would look like:

Code: Select all

SELECT * FROM blog WHERE (blog_date >= '2011-03-01' AND blog_date <= '2011-03-31')
HTH,

Mecha Godzilla
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

Re: list last ten months (blog archieves like)

Post by fael097 »

alright, fantastic! thank you
so heres my code:

Code: Select all

<!--PHP ARCHIEVES START-->
<?php
$today=getdate();
$day=$today['mday'];
$month=$today['mon'];
$year=$today['year'];
$months_menu="<ul>";

for ($i = 0; $i < 6; $i++)
{
    $month = str_pad((int) $month,2,"0",STR_PAD_LEFT); // pad the month so it is formatted as two digits
    if ($month != 0)
	{
        $months_menu .="<li><a href='#'>".$month."/".$year."</li>";
        $month--;
	}
	else
	{
        $month = 12;
        $year--;
        $months_menu .="<li><a href='#'>".$month."/".$year."</li>";
        $month--;
    }
}
$months_menu .="<li><a href='#'>Older...</li></ul>";

echo $months_menu;
?>
<!--PHP ARCHIEVES END-->
I decided to show 6 months only, and then put a link to older entries. what you think?

im just trying to replace months numbers to names now. a str replace on the $month variable will do it?
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: list last ten months (blog archieves like)

Post by mecha_godzilla »

That code looks fine - I tend to use <select> elements to save space but using <li> is probably more in keeping with how blogs work, plus it's better from a search engine perspective as long as you retrieve pages with a url like this:

Code: Select all

www.mydomain.com/getblog.php?month=april&year=2011
A quick way to generate the names for each month is just to put them in an array, but PHP has in-built functions to do the same thing. I'm sure date() can probably do this but you'd better check that first!

Good luck with your project anyway - if you need any more input please say so :)

M_G
Post Reply