简体   繁体   English

如何使用cakephp从文件夹中对文件进行分页

[英]How to paginate Files from a Folder using cakephp

Basically I want to paginate the contents of a folder using the Folder/File classes from CakePHP. 基本上,我想使用CakePHP中的Folder / File类对文件夹的内容进行分页。

I have this in my controller: 我的控制器中有这个:

$dir = new Folder('img/gallery/'.$gallery[0]['Gallery']['path']);
$file = $dir->find('.*', true);
$this->set('files', $file);

Now I want to paginate the results. 现在我要对结果进行分页。 They look like this: 他们看起来像这样:

array (size=188)
0 => string '52652681-023c-4e51-99b5-16cccbdd56cb.jpeg' (length=41)
1 => string '526f4034-9b6c-47c8-94f9-10e0cbdd56cb.jpeg' (length=41)
2 => string 'DSCF0232.JPG' (length=12)
3 => string 'DSCF0233.JPG' (length=12)
4 => string 'DSCF0234.JPG' (length=12)

How can I? 我怎样才能?

you could use simple php code to do that. 您可以使用简单的php代码来做到这一点。 calculate first the total of pages: 首先计算页面总数:

$pageSize = 10;    
$totalPages = (count($file) + $pageSize - 1) / $pageSize;

$this->set('totalPages ', $totalPages);

In your template already you can do this 您已经可以在模板中执行此操作

for($i = 1; $i <= $totalPages; $i ++){
    echo '<a href="YOUR_URI?page='.$i.'">'.$i.'</a>';
}

Now when a user click on the link sending the page he wants to see you will show only that part of the array and not all $file list. 现在,当用户单击发送页面的链接时,他只希望看到该页面,而不会显示所有$file列表。

$page = intval($_GET['page']);
$dir = new Folder('img/gallery/'.$gallery[0]['Gallery']['path']);
$file = $dir->find('.*', true);
$pageSize = 10;    
$totalPages = (count($file) + $pageSize - 1) / $pageSize;
$index = $page * $pageSize;
$file_list = array();
for($i = 0; $i < $pageSize; $i ++){
   if(isset($file[$index+$i]))
   $file_list[] = $file[$index+$i];
}
$this->set('totalPages ', $totalPages);
$this->set('files', $file_list);

This is the idea. 这是主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM