2012-11-04 75 views
4

我想分頁查看自定義數組。我的控制器和視圖代碼如下。如何在CakePHP中分頁自定義數組

控制器代碼:

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 name。但是我怎麼能爲上面的代碼做到這一點?

誰能幫我請..

+0

我認爲Pbscodemetric.created爲DATE類型,而不是DATETIME的? –

回答

2

您可以實現自定義分頁,見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`)', $alias), 
    '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); 

這會給你Paginator helper兼容的結果與總結downloads_iphonedownloads_ipaddownloads_android列,由created列分組。它採用了虛擬領域,以中檢索默認的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; ?>