dongxin8392 2013-11-08 09:10
浏览 19
已采纳

数据库结果按月和年

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>
  • 写回答

1条回答 默认 最新

  • douhuan1648 2013-11-08 09:32
    关注

    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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀