0

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

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

现在代码显示目录中的所有图像,每行 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>";
    }
}

?>

任何帮助都感激不尽!

提前谢谢了!!

4

3 回答 3

0

The general approach would be to count the total number of available files (which you are currently doing). Then decide how may files per page you are going to show (in your case 5). Divide the count by that value to show per page to determine how many pages are needed (i.e. what "1,2,3, next" output should be). Then have each link for 1,2,3, next link back to the same page with a parameter passed. So for example the link for page 2 might be /yourscript.php?page=2

You would then use the page offset to know where within your file array to being output. So from the example if page = 2, you would start you count on array offset 5 (because files 0-4 are on the first page).

于 2012-08-13T17:10:17.500 回答
0

尝试这个: -

输出

在此处输入图像描述

<?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;
?>
于 2012-08-13T17:41:27.993 回答
0

DirectoryIterator并且LimitIterator是我最好的新朋友,虽然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,而globexample 只是磁盘路径。

于 2013-01-31T02:20:54.297 回答