如何在scandir()php中添加分页 [英] How to add Pagination in scandir() php

查看:57
本文介绍了如何在scandir()php中添加分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在'scandir'php代码中添加分页,请帮助我如何在我的php代码中添加分页

I want to add pagination my 'scandir' php code , please help me how to add pagination in my php code

这是我的代码

    <?

// image extensions
$extensions = array('3gp', 'mp4', 'png', 'gif', 'bmp');

// init result
$result = array();

// directory to scan
$directory = new DirectoryIterator('files/'.$_GET['dir'].'');

// iterate
foreach ($directory as $fileinfo) {
    // must be a file
    if ($fileinfo->isFile()) {
        // file extension
        $extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
        // check if extension match
        if (in_array($extension, $extensions)) {
            // add to result
            $result[] = $fileinfo->getFilename();
        }
    }
}
// print result

foreach($result as $files)
{

    echo "<div class='fl odd".(++$j%2==0 ? "2" : "")."'>";
    echo '<a class="fileName" href="file.php?file=files/'.$_GET['dir'].'/'.$files.'"><div><div><img src="thumb.php?dir=files/'.$_GET['dir'].'/'.$files.'" width="80" height="80" alt="'.$files.'" /></div><div>'.$files.'<br/><span>[Size : 32.74Mb]</span><br/></div></div></a></div>';}

现在,请向我解释如何在此代码中添加分页(页面),我希望每页显示10个结果.

Now , please explain me how to add Pagination (Pages) in this code , i want to show 10 results per pag.

推荐答案

尝试以下示例:

假设我们有index.php文件

let's say we have index.php file

$perpage = 10;
$page = (int)$_GET['page'];
if(!($page>0)) $page = 1;
$offset = ($page-1)*$perpage;

$extensions = array('3gp', 'mp4', 'png', 'gif', 'bmp');
$files = glob('files/'.$_GET['dir'].'/*.'.'{'.implode(',', $extensions).'}', GLOB_BRACE);
$total_files = sizeof($files);
$total_pages = ceil($total_files/$perpage);
$files = array_slice($files, $offset, $perpage);
?>

<div>
    SHOWING: <?=$offset?>-<?=($offset+$perpage)?>  of <?=$total_files?> files
</div>

<?php foreach($files AS $file) : ?>
    <a class="fileName" href="file.php?file=files/<?=$_GET['dir']?>/<?=basename($file)?>">
        <?=basename($file)?>
    </a>
<?php endforeach; ?>




<a class="page" href="index.php?dir=<?=$_GET['dir']?>&page=<?=(($page-1>1)?($page-1):1)?>">
   <<
</a>

<?php for($p=1; $p<=$total_pages; $p++) : ?>
    <a class="page" href="index.php?dir=<?=$_GET['dir']?>&page=<?=$p?>">
        <?=$p?>
    </a>
<?php endfor; ?>

<a class="page" href="index.php?dir=<?=$_GET['dir']?>&page=<?=(($page+1>$total_pages)?$total_pages:($page+1)?>">
   >>
</a>

这篇关于如何在scandir()php中添加分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆