cakephp如何按日期时间进行分页组,但保留所有数据

时间:2013-03-14 22:24:46

标签: php mysql cakephp pagination cakephp-2.0

所以,如果我有一个usehistory数据库表,如

id     file   email   date_send
1     1.txt   A        2013-01-31 15:26:00  
2     2.txt   A        2013-01-31 15:26:00
3     3.txt   B        2013-01-30 12:26:00

我希望在视图中显示

Date                   email        File
2013-01-31 15:26:00     A           1.txt
                                    2.txt
2013-01-30 12:26:00     B           3.txt

我怎么能这样做,考虑到cakephp中的分页。 我现在所拥有的只是将其显示为

Date                   email        File
2013-01-31 15:26:00     A           1.txt
2013-01-31 15:26:00     A           2.txt
2013-01-30 12:26:00     B           3.txt

我在控制器中的代码

        $this->paginate =  array(
            'UsageHistory' => array(
                'conditions' => array(
                    'UsageHistory.user_id' => $id
                ),
                'order' => 'UsageHistory.date_send DESC'
            )
        );
        $this->set('usageHistory', $this->paginate('UsageHistory'));

并在视图中

<?php if ( !empty($usageHistory) ) { ?>
                            <?php foreach ( $usageHistory as $history ) :?>  
                        <tr>
                            <td class="filename"><?php echo $history['UsageHistory']['file_name']; ?></td>
                            <td class="recip"><?php echo $history['UsageHistory']['recipient_email']; ?></td>
                            <td class="size"><?php echo round($history['UsageHistory']['file_size']/1048576*100)/100 . ' MB'; ?></td>
                            <td class="datesent"><?php echo $this->Time->format('m/d/o h:i A', $history['UsageHistory']['date_send']); ?></td>
                        </tr>
                            <?php endforeach; ?>
                        <?php } else { ?>
                        <tr>
                            <td colspan="3">No Usage History Yet</td>
                        </tr>
                        <?php } ?>

1 个答案:

答案 0 :(得分:0)

这纯粹是数据的演示文稿,因此应该在视图中完成。

因为您仍然希望所有行都可见,所以不必修改查询,只需要显示结果的方式。

“虚拟”代码

$curDate = null;

foreach($results as $row) {
   if($row['date'] != $curDate) {
       // new date/time; should be visible
       $visibleDate = $row['date'];

       // Make this the new 'current' date
       $curDate = $row['date'];
   } else {
       // Still the same date; should not be visible
       $visibleDate = '';
   }
   ?>
   <tr><td><?php echo $visibleDate ?></td><td><?php echo $row['email'] ?></td><td>...</td></tr>
   <?php
}

“电子邮件”栏

也可以这样做