Need one header per category from an array

Discussion in 'PHP' started by scottishlass, Mar 12, 2008.

  1. #1
    I am pulling membership listings from a database for a 3 month interval. Below is code to put the month as the header of each entry. I need to put the month as the header of each SECTION of entries for that month. Can someone help please?

    
    $sql = "SELECT id, first_name, last_name, company, date_format(entry_date, '%m-%d-%Y') as formatted_date, date_format(entry_date, '%M') as formatted_month FROM {$mbsp_tablename} WHERE entry_date BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 2 MONTH ) AND CURDATE( ) ORDER BY entry_date, last_name";
    							
    $result2 = mysql_query($sql) or die(mysql_error()." - error: could not connect");
    							
    while ($data = mysql_fetch_array($result2)) {
    	echo "<span class='months'>".$data['formatted_month']."</span>";
    	echo "<p class='newbies'><strong>".$data['first_name']." ".$data['last_name']."</strong><br />".$data['company']."</p>";
    }
    
    Code (markup):
    Thanks!

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    scottishlass

    "I was put on Earth to accomplish a certain number of things. Right now, I am so far behind, I will never die." - Calvin, Calvin & Hobbes
     
    scottishlass, Mar 12, 2008 IP
  2. mulari

    mulari Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is the way I do group printing.

    Because you order the results by date, it should be shown fine (only once per section).

    $month = '';
    while ($data = mysql_fetch_array($result2)) {
        if ($month != $data['formatted_month'])
        {
            $month = $data['formatted_month'];
            echo "<span class='months'>".$data['formatted_month']."</span>";
        }
        echo "<p class='newbies'><strong>".$data['first_name']." ".$data['last_name'
    ]."</strong><br />".$data['company']."</p>";
    }
    
    Code (markup):
     
    mulari, Mar 12, 2008 IP