每月创建表

Assuming I have the following MySQL table;

id      | title   | date
--------+---------+----------
0       | john doe| 2013-02-01
2       | tarzan  | 2013-04-01
9       | toy-box | 2013-04-02
11      | jane    | 2013-01-01

I want to fetch all these results, and per month-year I want to create a HTML table:

January, 2013
[*] Jane

February, 2013
[*] John doe

April, 2013
[*] Tarzan
[*] toy-box

As you can see, March does not exist. How can I produce something like this with PHP?

Just order by month (and maybe title) using SQL. Then do something like this:

$lastMonthId = null;

foreach ($records as $record) {
    $date = strtotime($record['date']);

    $monthId = date('Ym', $date);
    if ($monthId !== $lastMonthId) {
        if ($lastMonthId !== null) echo '</table>';
        echo '<h1>', date('Y-m', $date), '</h1>';
        echo '<table>';

        $lastMonthId = $monthId;
    }

    // Output <tr> for this record.
}

echo '</table>';

This starts a new table every time the month changes from one record to the next.

This, of course, doesn't display empty months because they are not present in the dataset.

Also, it kinda looks like you actually want a list here. If that is the case, just replace <table>, </table> and <tr> in that snippet with <ul>, </ul> and <li> respectively.

Create an array with Key as month , if you find any record for that month add to the array . At the end you will have array with month as key and all the names in another array

         $myData = array()
         while(Looping through DB record)
        {
             extract Month part and check array_key_exists($myData , monthPart)
            if yes then $myData[$key]['Name'] = UserName
        }