我如何在CakePHP中分页自定义数组
发布时间:2020-12-13 16:19:07 所属栏目:PHP教程 来源:网络整理
导读:我想在视图中对自定义数组进行分页.我的控制器和视图代码如下. 控制器代码: public function admin_detail(){ $totalByDate = $this-Pbscodemetric-find('all',array('fields'=array('created'))); $createdDate = Set::extract('/Pbscodemetric/created',$
我想在视图中对自定义数组进行分页.我的控制器和视图代码如下.
控制器代码: public function admin_detail(){ $totalByDate = $this->Pbscodemetric->find('all',array('fields'=>array('created'))); $createdDate = Set::extract('/Pbscodemetric/created',$totalByDate); $uniqueCreatedDate = array_unique($createdDate); $finalArray = array(); foreach($uniqueCreatedDate as $created){ $downloadsArr = $this->Pbscodemetric->find('all',array('fields'=>array('downloads_iphone','downloads_ipad','downloads_android'),'conditions'=>array('created'=>$created))); $download_iphone = array_sum(Set::extract('/Pbscodemetric/downloads_iphone',$downloadsArr)); $download_ipad = array_sum(Set::extract('/Pbscodemetric/downloads_ipad',$downloadsArr)); $downloads_android = array_sum(Set::extract('/Pbscodemetric/downloads_android',$downloadsArr)); $finalArray[$created] = array( 'downloads_iphone' => $download_iphone,'downloads_ipad' => $download_ipad,'downloads_android' => $downloads_android ); } $this->set('finalArray',$finalArray); } 查看代码: <div class="pbscodemetrics index"> <h2><?php echo __('Pbscodemetrics List Detail'); ?></h2> <table cellpadding="0" cellspacing="0"> <tr> <th>Date</th> <th>iPhone</th> <th>iPad</th> <th>Android</th> </tr> <?php foreach($finalArray as $key => $final){?> <tr> <td><?php echo $key;?></td> <td><?php echo $final['downloads_iphone'];?></td> <td><?php echo $final['downloads_ipad'];?></td> <td><?php echo $final['downloads_android'];?></td> </tr> <?php }?> </table> </div> <div class="actions"> <?php echo $this->element('nav');?> </div> 现在我想设置分页.我知道在CakePHP默认分页需要Model名称.但是我怎么能为上面的代码做到这一点? 有人可以帮我吗… 解决方法
您可以实现自定义分页,请参阅Cookbook中的
Custom Query Pagination.但是,看起来你在那里做的事情可以使用分组和SQL SUM函数完成,这样你就可以简单地使用内置的分页.例:
$alias = $this->Pbscodemetric->alias; $this->Pbscodemetric->virtualFields = array ( 'downloads_iphone_sum' => sprintf('SUM(`%s.downloads_iphone`)',$alias),'downloads_ipad_sum' => sprintf('SUM(`%s.downloads_ipad`)','downloads_android_sum' => sprintf('SUM(`%s.downloads_android`)',$alias) ); $this->paginate = array ( 'fields' => array ( 'downloads_iphone_sum','downloads_ipad_sum','downloads_android_sum','created' ),'group' => 'created','order' => array ( 'created' => 'desc' ),'limit' => 10 ); $data = $this->paginate($alias); 这将为您提供与所创建的列分组的总和downloads_iphone,downloads_ipad和downloads_android列的Paginator helper兼容结果.它使用虚拟字段以便以默认的CakePHP样式检索结果,即它们可以像这样访问: <?php foreach($results as $result): ?> <tr> <td><?php echo $result['Pbscodemetric']['created'];?></td> <td><?php echo $result['Pbscodemetric']['downloads_iphone_sum'];?></td> <td><?php echo $result['Pbscodemetric']['downloads_ipad_sum'];?></td> <td><?php echo $result['Pbscodemetric']['downloads_android_sum'];?></td> </tr> <?php endforeach; ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |