Reputation: 325
I have a Joomla 3.1 site that is going to show reports about contributions made. The reports should be divided into groups by month and year. I have a code that pulls data from database but cannot make it put each month into a separate <div>
and each year into a parent <div>
to its months. How is it done? Here is my code.
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('contribution_date'), $db->quoteName('contribution_amount'), $db->quoteName('source')));
$query->from($db->quoteName('contribution'));
$db->setQuery($query);
$results = $db->loadObjectList();
?>
<table class='financial-reports'><th>aaa</th><th>bbb</th><th>ccc</th>
<?php
foreach ( $results as $result) {
echo '<tr>' . '<td class="fin-rep-date">' . JFactory::getDate($result->contribution_date)->format('d M, Y') . '</td>' .
'<td class="fin-rep-sum">' . $result->contribution_amount . ' ' .'руб.' . '</td>' .
'<td class="fin-rep-source">' . $result->source . '</td>' . '</tr>';
}
?>
</table>
Upvotes: 0
Views: 312
Reputation: 2012
I think the easies way would be convert results into multi-dimensional array like this:
foreach ($results as $result) {
$resultsArray[JFactory::getDate($result->contribution_date)->format('Y')][JFactory::getDate($result->contribution_date)->format('M')][] = $result;
}
And then you can use it like that:
<?php foreach ($resultsArray as $year) : ?>
<div>
<?php foreach ($year as $month) : ?>
<div>
<?php foreach ($month as $item) : ?>
<div>YOUR OUTPUT <?php echo $item->contribution_date; ?></div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
Upvotes: 3