Need one header per category from an array

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
scottishlass
Forum Newbie
Posts: 3
Joined: Wed Mar 12, 2008 8:37 pm

Need one header per category from an array

Post by scottishlass »

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?

Code: Select all

 
$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>";
}
 
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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Need one header per category from an array

Post by Christopher »

How do you determine "each SECTION"? I don't see a column for that.
(#10850)
scottishlass
Forum Newbie
Posts: 3
Joined: Wed Mar 12, 2008 8:37 pm

Re: Need one header per category from an array

Post by scottishlass »

Each "section" is the month as indicated by "date_format(entry_date, '%M') as formatted_month". What I need output-wise (if January is the first month of the interval required) is:

January
Name
Company

Name
Company

(more if in db)

February
Name
Company

Name
Company

(more if in db)

March
Name
Company

Name
Company

(more if in db)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
Forum Newbie
Posts: 3
Joined: Wed Mar 12, 2008 8:37 pm

Re: Need one header per category from an array

Post by scottishlass »

Found the answer:

$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");

$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>";
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Need one header per category from an array

Post by Christopher »

Typically for heading you use logic like this:

Code: Select all

$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");
                            
$last_month = '';
while ($data = mysql_fetch_array($result2)) {
    if ($data['formatted_month'] != $last_month) {
        echo "<span class='months'>".$data['formatted_month']."</span>";
        $last_month = $data['formatted_month'];
    }
    echo "<p class='newbies'><strong>".$data['first_name']." ".$data['last_name']."</strong><br />".$data['company']."</p>";
}
(#10850)
Post Reply