How can I configure a custom array in CakePHP

I want pagination for a specific array. My controller and view code are below.

Controller Code:

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); } 

View code:

 <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> 

Now I want to set the pagination. I know CakePHP requires a model name to paginate. But how can I do this for the above code?

Can anyone help me ..

+4
source share
1 answer

You can implement custom pagination, see Custom Pagination in the Cookbook. However, it looks like what you are doing can be accomplished with grouping and the SQL SUM function so that you can just use the built-in pagination. Example:

 $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); 

This will give you compatible Paginator results with the summarized columns downloads_iphone , downloads_ipad and downloads_android columns grouped by created . It uses virtual fields to return CakePHP-style default results, meaning they will be available as follows:

  <?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; ?> 
+2
source

Source: https://habr.com/ru/post/1443921/


All Articles