Page 1 of 1

[SOLVED] Foreach the wrong thing to be using?

Posted: Mon Apr 07, 2008 11:02 am
by lafever
I'm trying to add another style of archiving for different 'themes' and the loop I am using is running through it over and over until it hits each month, instead of hitting them all at once. I've tried playing with some other loops but I either get the wrong data or the same data.

Code: Select all

$results = mysql_query("SELECT DISTINCT YEAR(date) AS `year`, DATE_FORMAT(date, '%m') AS `month` FROM blog_posts $where GROUP BY YEAR(date), DATE_FORMAT(date, '%m') ORDER BY date DESC", $connection);
 
$posts = array();
while ($row = mysql_fetch_assoc($results)) {
    if (!isset($posts[$row['year']][$row['month']])) {
       $posts[$row['year']][$row['month']] = array();
    }
    $posts[$row['year']][$row['month']][] = $row;
}
 
$month_array = array("01" => "jan", "02" => "feb", "03" => "mar", "04" => "apr",
                    "05" => "may", "06" => "jun", "07" => "jul",
                    "08" => "aug", "09" => "sep", "10" => "oct", "11" => "nov", "12" => "dec");
 
 
$i=0;
foreach ($posts as $year => $yelement) {
    echo '<div id="archcal"><ul>';
    echo '<li class="year">'.$year .'';
 
        foreach ($yelement as $month => $melement) {
            foreach ($month_array as $mon_key => $month2) {
              $class = ($i%2) ? 'class="light"' : 'class="dark"';
                if ($month == $mon_key) {
                    echo '<li '.$class.'><a href='.$month.'>'. $month2 .'</a></li>';
                } else {
                    echo '<li '.$class.'>'.$month2.'</li>';
                }
              $i++;
            }
        }
    echo '</ul></div>';
}
What it's doing is I have 3 different months entered for 2008 so it's displaying (the ones in brackets are the linked ones)

Code: Select all

 
    2008
  jan  [feb]  mar
  apr  may jun
  jul   aug  sep
  oct  nov  dec
  jan  feb  [mar]
  apr  may jun
  jul   aug  sep
  oct  nov  dec
  jan  feb  mar
  [apr]  may jun
  jul   aug  sep
  oct  nov  dec
 
When it should be displaying:

Code: Select all

 
        2008
  jan  [feb] [mar]
 [apr] may jun
  jul   aug  sep
  oct  nov  dec
 

Re: Foreach the wrong thing to be using?

Posted: Mon Apr 07, 2008 11:23 am
by John Cartwright
You had your logic and loop backwards.

You want to loop the months and check if that months exists in your $post array, preferably using array_key_exists() or isset()

Code: Select all

 
$i=0;
foreach ($posts as $year => $yelement) {
    echo '<div id="archcal"><ul>';
    echo '<li class="year">'.$year .'</li>';
    
    $class = ($i%2) ? 'class="light"' : 'class="dark"';
    $month_array = array(
        "01" => "jan", "02" => "feb", "03" => "mar", "04" => "apr",
        "05" => "may", "06" => "jun", "07" => "jul",
        "08" => "aug", "09" => "sep", "10" => "oct", "11" => "nov", "12" => "dec"
    );
                
    foreach ($month_array as $monthnum => $monthname) {
        if (array_key_exists($monthnum, $yelement)) {
            echo '<li '.$class.'><a href='.$monthnum.'>'. $monthname .'</a></li>';
        } else {
            echo '<li '.$class.'>'. $monthname .'</li>';
        }
        $i++;
    }
    echo '</ul>';
}