I would reconsider breaking out of PHP every time you need to print HTML tags. It makes your code hard to read.
Also, you're doing a query within a loop, which is a bad idea, and looks completely unnecessary for your problem.
You also have an opening <tbody> tag in your loop with no closing </tbody> tag.
And, as @AleksG mentioned in the comments, you shouldn't use mysql_* functions as they are deprecated. I recommend switching to PDO.
So on to the answer...
Assuming your table structure looks like this:
+-------+------+-------+
| month | year | dates |
+-------+------+-------+
| Jan | 2013 | 124 |
| Jan | 2013 | 243 |
| Jan | 2013 | 354 |
| Feb | 2013 | 978 |
| Feb | 2013 | 765 |
| Feb | 2013 | 756 |
+-------+------+-------+
Based on your question title, this will put the values into an array, with the keys being the month and year, and the values being the numeric "dates" field:
// replace the below line with your mysql connection string
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$result = $dbh->query('
SELECT month, year, dates FROM calendar ORDER BY year, month
');
$mnth = array();
foreach ($result as $row) {
$key = $row['month'] .','. $row['year']; // e.g. 'Jan,2013'
// push the new value. The inner array will be created automatically
$mnth[$key][] = $row['dates'];
}
print_r($mnth);
/* will look like this:
Array(
[Jan,2013] => Array(
[0] => 124,
[1] => 243,
[2] => 354
),
[Feb,2013] => Array(
[0] => 978,
[1] => 765,
[2] => 756
)
) */
But based on your code, it looks like you want to output this as an HTML table. So here's how you could do that. The query is the same, so this just replaces the loop:
echo '<table>';
$current = '';
foreach ($result as $row) {
// check to see if we've switched to a new month/year
$next = $row['month'] .','. $row['year'];
if ($current != $next) {
// if we have moved to a new month/year, print a new header row
echo '<tr><th>'. $next .'</th></tr>';
// and update $current
$current = $next;
}
echo '<tr><td>'. $row['dates'] .'</td></tr>';
}
echo '</table>';
mysql_functions - they are deprecated. Switch tomysqli_or PDO.