【问题标题】:Paging listed files from a directory从目录中分页列出的文件
【发布时间】:2012-08-09 22:56:21
【问题描述】:

我是网页编码的新手,但我遇到了问题...

现在我可以添加一个代码来列出和回显目录中的图像,用灯箱显示它们。

现在代码显示目录中的所有图像,每行 5 张图像,但没有限制...

如何将我的代码更改为页面结果,例如:只有一行,每页 5 张图片。然后显示通常的“Page: 1, 2, 3 ... Next ...” 我真的不知道该怎么做...我尝试了很多事情都没有成功...

代码如下:

<?php 

$page = $_SERVER['PHP_SELF'];

// How many images per row
$maxCols = 5;

// Directory where the albums are stored
$base = "albums";

// Get album title
$get_album = $_GET['album'];


if (!$get_album)
{
    $handle = opendir($base);

    echo "<div id='albums' class='imageRow'>";

    while (($file = readdir($handle))!==FALSE)
    {       
        if (is_dir($base."/".$file) && $file != "." && $file != "..")
        {
            //$list []= $file;
            $img = $file.".jpg"; //Main image from each album.

            echo "<div class='image'><a href='$page?album=$file'><img src='thumbnails.php?img=$base/$img' alt='$file' /></a><div class='album'><i>$file</i></div></div>";
        }       
    }

    echo "</div>";

    closedir($handle);

    //$total = count($list);

}
else
{
    if (!is_dir($base."/".$get_album) || strstr($get_album,".")!=NULL || strstr($get_album,"/")!=NULL || strstr($get_album,"\\")!=NULL)
    {
        echo "Album doesn't exist.";
        echo "<p /><a href='$page'>Back to albums</a>";
    }
    else
    {
        $count = 0;
        $handle = opendir($base."/".$get_album);

        echo "<div id='images' class='imageRow'>";

        while (($file = readdir($handle)) !== FALSE)
        {
            if ($file != "." && $file !== "..")
            {
                echo "<div class='image'><a href='$base/$get_album/$file' rel='lightbox[1]' title='$file'><img src='thumbnails.php?img=$base/$get_album/$file' alt='$file' /></a></div>";

                $count++;

                if($count == $maxCols) 
                {
                    echo "</div>";
                    echo "<div id='images' class='imageRow'>";
                    $count = 0;
                }

                $list[] = $file; //Assign the images to a list to allow count.
            }
        }

        echo "</div>";

        closedir($handle);

        $total = count($list); //Total elements at the album.

        //echo "<a href='$page'>Back to albums</a>";
    }
}

?>

任何帮助将不胜感激!

提前非常感谢!!

【问题讨论】:

  • 感谢您的回答,我似乎无法将该代码用于我的目的,我可以用它进行分页,但仍然可以显示我的目录中的每个文件。

标签: php html


【解决方案1】:

一般方法是计算可用文件的总数(您目前正在这样做)。然后决定每页要显示多少文件(在您的情况下为 5)。将计数除以该值以显示每页以确定需要多少页(即“1、2、3、下一个”输出应该是什么)。然后让每个链接为 1、2、3,下一个链接返回到同一页面,并传递一个参数。例如,第 2 页的链接可能是 /yourscript.php?page=2

然后,您将使用页面偏移量来了解要在文件数组中输出的位置。因此,如果页面 = 2,则从示例开始,您将开始计算数组偏移量 5(因为文件 0-4 在第一页上)。

【讨论】:

  • 您好,感谢您的回答,我知道分页的概念,实际上我已经编写了一个分页系统,它可以获取 MySQL 寄存器,并且可以完美运行。现在的问题是我通过扫描目录并输出该目录中的每个文件来获得结果。我试图弄清楚如何添加我拥有的分页系统,但我似乎无法实现它
【解决方案2】:

试试这个:--

输出

<?php

$maindir = "img" ;
$mydir = opendir($maindir) ;
$limit = 5;
$offset = ((int)$_GET['offset']) ? $_GET['offset'] : 0; 
$files = array();
$page='';
$exclude = array( ".", "..", "index.php",".htaccess","guarantee.gif") ;
while($fn = readdir($mydir))
{
    if (!in_array($fn, $exclude)) 
    {
        $files[] = $fn;;
    }
}
closedir($mydir);
sort($files);
$newICounter = (($offset + $limit) <= sizeof($files)) ? ($offset + $limit) : sizeof($files);

for($i=$offset;$i<$newICounter;$i++) {  
?>
    <a href="<?php print $files[$i]; ?>"><?php print $files[$i]; ?></a><br>
<?php
}
freddyShowNav($offset,$limit,sizeof($files),"");

function freddyShowNav($offset, $limit, $totalnum, $query) {
    global $PHP_SELF;
    if ($totalnum > $limit) {
            // calculate number of pages needing links 
            $pages = intval($totalnum/$limit);

            // $pages now contains int of pages needed unless there is a remainder from division 
            if ($totalnum%$limit) $pages++;

            if (($offset + $limit) > $totalnum) {
                $lastnum = $totalnum;
                }
            else {
                $lastnum = ($offset + $limit);
                }
            ?>
                <table cellpadding="4"><tr><td>Page </td>
            <?php
            for ($i=1; $i <= $pages; $i++) {  // loop thru 
                $newoffset=$limit*($i-1);
                if ($newoffset != $offset) {
            ?>
                    <td>
                        <a href="<?php print  $PHP_SELF; ?>?offset=<?php print $newoffset; ?><?php print $query; ?>"><?php print $i; ?>
                        </a>
                    </td>
            <?php
                    }     
                else {
            ?>
                    <td><?php print $i; ?></td>
            <?php
                    }
                }
            ?>
                    </tr></table>
            <?php
        }
    return;
    }



echo $page;
?>

【讨论】:

  • 谢谢,但是太长太复杂,无法适应我的代码,而且它做的事情与我需要的不同。 ;)
【解决方案3】:

DirectoryIteratorLimitIterator 是我最好的新朋友,尽管glob 似乎更容易进行预过滤。你也可以写一个自定义的FilterIterator。我认为需要 PHP > 5.1。

无前置过滤器:

$dir_iterator = new DirectoryIterator($dir);
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage);

全局预过滤器:

$dir_glob = $dir . '/*.{jpg,gif,png}';

$dir_iterator = new ArrayObject(glob($dir_glob, GLOB_BRACE));
$dir_iterator = $dir_iterator->getIterator();
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage);

那么,做你的事:

foreach ($paginated as $file) { ... }

请注意,在DirectoryIterator 示例中,$file 将是SplFileInfo 的一个实例,而glob 示例只是磁盘路径。

【讨论】:

    猜你喜欢
    • 2012-12-09
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2013-08-30
    • 2012-03-22
    • 2010-12-04
    • 1970-01-01
    相关资源
    最近更新 更多