0

所以,如果我有一个使用历史数据库表,比如

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

1 回答 1

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
}

'email' 列也可以这样做

于 2013-03-14T23:04:27.867 回答