Jump to content

Displaying Archives


timmy0320

Recommended Posts

I am having some troubles figuring out how to display an 'Archive' of my posts in my table. I'm reading the SQL manual and I'm pretty sure that I have the SQL syntax down but I'm not quite sure how to implement it into PHP exactly.

 

 

What I'm trying to do is display something resulting like below:

 

2008

  - March

    -(date)title

    -(date)title

 

 

This is what I have so far

<?php
                // check to see if just the year is set or both
                if ($month != '') {
                  $sql = "SELECT * FROM blog_posts WHERE YEAR(date) LIKE '$year' AND DATE_FORMAT(date, '%m') LIKE '$month'";
                } else {
                  $sql = "SELECT * FROM blog_posts WHERE YEAR(date) LIKE '$year'";
                }

             $res = mysql_query($sql, $connection);


            while ($row = mysql_fetch_array($res)){
               // only check if there is one
               $year = date('Y', strtotime($row['date']));
               $month = date('m', strtotime($row['date']));
               $num = mysql_num_rows($res);
               echo "".$year." (".$num.")<br>";
            }

?>

 

It's displaying:

2008 (2)

2008 (2)

 

 

I understand why it's doing that and I know this code is all jacked up but should I be doing a foreach instead of a while and how would the indentions go on this?

 

I also think that my SQL syntax is going to have to change by the way I am trying to do this.

 

Thanks for all the help.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.