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 に答える