I've been trying to figure out this bit of code for 2 days and I can't figure it out. As written by the instructor:
Code:
Since the index page only shows the latest posts, we will need some organized means to
display all of the posts in the system. One way to do this is through the posts summary
page.
The posts summary should organize posts by year, then by month.
For each post, simply display the title of the post. Clicking on the post will take the
user to a page that shows that post and only that post. (See the reference blog web
site for a hint of how this is being done using $ GET).
Near the top of the page, don't forget to display the total number of posts that the
blog has so far.
This page is probably the most dicult page of the project, and it is tricky for several
reasons.
Remember that you can get just the year or just the month out of a date eld by performing
the following:
$post_year = date("Y", strtotime($row['post_date']));
$post_month = date("F", strtotime($row['post_date']));
This might, for example, put 2011 in the variable $post year and the string January in
$post month.
One approach to solving this problem in general is to return a set of results that are sorted by
post date in descending order. Keep some temporary variables, perhaps $year and $month
and initialize them to NULL.
Then, iterate (using a while) through each of the records, comparing $post year with $year
and $post month with $month. If they are dierent, output (using echo) the new year or
month and set $post year and $post month to the last seen $year and $month.
In this way, every time a new year or month is encountered a new heading will be output as
HTML to the screen.
I managed to get the links to the individual post working in 30 mins, bit the loops to output the everthing else is giving me problems. It's supposed to be structured like:
Code:
Year
---Month
------Post
Year
---Month
------Post
My code is posted below. Any help would be
greatly appreciated. The site is accessible at
http://dragonfall.ath.cx/final/.