2013-10-25 84 views
0

我有这样的脚本:执行opendir()支持分页

<?php 
if ($handle = opendir('konten')) { 
    $blacklist = array('.', '..', 'somedir', 'index.php', 'style.css'); 
    while (false !== ($file = readdir($handle))) : 
     if (!in_array($file, $blacklist)) : 

?> 
<div style="display:block;clear:both;"> 
<span style="font-size:18px;">&raquo;</span> <a href=""><?php echo $file;?></a> 
</div> 
<?php 
endif; 
endwhile; 
closedir($handle); 
} 
?> 

输出看起来是这样的:

» linux (5th copy) 
» linux 
» linux (10th copy) 
» linux (9th copy) 
» linux (4th copy) 
» linux (6th copy) 
» linux (8th copy) 
» linux (copy) 
» linux (7th copy) 
» linux (another copy) 
» linux (3rd copy) 

如何分页补充呢?
例如: 我想在一个页面上只显示3个dirs。

回答

1

其实你需要设置限制和页面类似如下:

但在这种情况下,你会得到几乎目录中的所有文件。

<?php 
$limit = 4; //Or just for dynamic limit - (int)$_GET['limit']; 
$page = (int)$_GET['page']?:0; // _GET['page'] or 0 for default 
$skip = $limit * $page; 
if ($handle = opendir('konten')) { 
    $blacklist = array('.', '..', 'somedir', 'index.php', 'style.css'); 
    $skiped = 0; 
    while (false !== ($file = readdir($handle))) { 
     if (!in_array($file, $blacklist)) { 
     $skipped++; 
     if ($skipped < $skip || $skipped >= $skip + $limit) { 
      continue; 
     } 
     ?> 
      <div style="display:block;clear:both;"> 
       <span style="font-size:18px;">&raquo;</span> <a href=""><?php echo $file;?></a> 
      </div>   
     <?php } 
    } 
} 
// For pagination support 
$pages = (int)$skipped/$limit; 
if ($skipped % $limit) 
    $pages ++; 

for ($i = 1; $i <= $pages; $i++) { 
    $class = ''; 
    if ($page == $i) $class = 'class="active"'; 
    ?> <a href="?page=<?= $i ?>" <?= $class ?>><?= $i ?></a> <?php 
} 
?> 

UPD:添加分页支持

+0

这就是工作,如果我例如设置网址到极限= 4&页= 1?。但如何显示分页链接? sory im noob in php like this – Anggagewor

+0

@AnggaMovic的帖子已更新。但是你需要在代码末尾更改cicle的URL。只有一个只有查询的临时网址。也有更新开始 - 没有更多的动态限制支持:) – Spell