Page 1 of 1

Need one header per category from an array

Posted: Wed Mar 12, 2008 8:39 pm
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

Re: Need one header per category from an array

Posted: Wed Mar 12, 2008 9:41 pm
by Christopher
How do you determine "each SECTION"? I don't see a column for that.

Re: Need one header per category from an array

Posted: Thu Mar 13, 2008 8:28 am
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

Re: Need one header per category from an array

Posted: Thu Mar 13, 2008 8:37 am
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

Re: Need one header per category from an array

Posted: Thu Mar 13, 2008 3:03 pm
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>";
}