0

我有一个 Joomla 3.1 网站,它将显示有关所做贡献的报告。报告应按月和年分组。我有一个从数据库中提取数据的代码,但不能将每个月放入一个单独的月份,<div>并将每年放入<div>其月份的父级。它是如何完成的?这是我的代码。

<?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>
4

1 回答 1

3

我认为最简单的方法是将结果转换为多维数组,如下所示:

foreach ($results as $result) {
    $resultsArray[JFactory::getDate($result->contribution_date)->format('Y')][JFactory::getDate($result->contribution_date)->format('M')][] = $result;
}

然后你可以像这样使用它:

<?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; ?>
于 2013-11-08T09:32:42.860 回答