Php 支持分页的opendir()

Php 支持分页的opendir(),php,pagination,Php,Pagination,我有这个剧本: <?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:b

我有这个剧本:

<?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);
}
?>
如何向其添加分页?
例如。:
我只想在一页上显示3个目录。

实际上,您需要设置限制和页面,如下所示:

但在这种情况下,您将获得目录中几乎所有的文件

<?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:添加了分页支持

例如,如果我将url设置为?limit=4&page=1,这就行了。但是如何显示分页链接呢?对不起,我不喜欢这样的phpone@AnggaMovic帖子更新了。但您需要在代码末尾更改cicle的URL。只有一个临时url,只有查询。开始时也有更新-不再支持动态限制:)
<?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
}
?>