【问题标题】:PHP - create pagination from an opendir array (photo gallery page)PHP - 从 opendir 数组创建分页(照片库页面)
【发布时间】:2015-07-04 23:41:35
【问题描述】:

我使用我的 MySQL 数据库完成了这项工作,但大约一年前,我将我的网站改造成更方便的摄影缩略图编码布局。其他人编写了代码,它使用 opendir 功能根据特定文件夹中的图像文件自动创建缩略图。

问题是,如果我有一个包含 100 多张图片的画廊,它会将所有图片加载到页面上,并且用户在加载完之前无法选择要查看的任何内容。

我想创建分页来加载,比如说,一次加载 15 个,然后允许用户转到其他页面(如果有的话)。因为我是 PHP 新手,所以这段代码是在我头上写的。

这是我认为需要更新的相关代码:

$dir = $dir.$gallery."/";

//Put files into an array
// create a handler to the directory
$dirhandler = opendir($dir);

// read all the files from directory

$nofiles=0;
while ($file = readdir($dirhandler)) {

// if $file isn't this directory or its parent 
//add to the $files array
if ($file != '.' && $file != '..')
{
$nofiles++;
$files[$nofiles]=$file;                
}   
}

//close the handler
closedir($dirhandler);

// sort folder names alphabetically, ignore case
natcasesort($files); 
?>

<div style="clear:both"></div>

<?
//Show images
foreach ($files as $file){   
if ($file!="."&&$file!="..")
{
$extention = explode('.', $file);
if ($extention[1] != "")
{       
echo "<div class='imgwrapper'>";
echo"<a class='fancybox' rel='group' href='$dir$file' return='false' title='$filename'>";
echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'>";
echo"</a><br>";
echo "</div>";
}
}
}   
?>

这甚至可能不是一切。老实说,我愿意花钱请人为我做这件事。我不知道这有多难或容易。如有必要,我可以发送整个页面或复制整个代码。

我昨天花了 7 个小时试图将我的网站转换为 WordPress 布局,其中有一个 Web 画廊插件已经可以做到这一点,但是移动布局是一团糟,它比这段代码已经很容易做到的要复杂一些。
我认为我当前的网站看起来和工作都很棒,但是这个分页是唯一缺少的东西。

【问题讨论】:

    标签: php limit photo opendir


    【解决方案1】:

    好的,你需要计算你拥有的文件数量

    $fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
    //if doesnt work replace __DIR__ with the path to your gallery
    $amountfile = (int) iterator_count($fi);
    // $amountfile is the number of file you got in one directory
    $amountpage = (int) $amountfile / 15 
    // this number will tell us how much page with 15 pictures you gonna have
    

    在页面的最后,我们需要创建一个小表单来访问其他页面

    <form action="THISFILE.php" method="GET">
    
    <?php
    
    for($i=0; $i<= $amountfile, $i++){
    
       echo "<input type="submit" name='page' value=";
       echo $i;   
       echo "'>";
    
    }   
    
    ?>
    
    </form>
    

    这将重新加载文件并通过 GET 页面的编号。接下来,您必须使用以下代码拦截 GET

    if($_GET['page'])
    {
       $choice = (int) $_GET['page'];
       $page = ($choice -1) * 15;
    }
    else
    {
       // in case we have not clicked on a specific page there is no GET    
       $page = 0;
    }
    

    然后我们只需要修改您的循环,使其仅显示 15 张图片,具体取决于您的页面选择。 所以想象一下你点击页面“2”它会做 -> (2-1) * 15
    因此,您的 $page 将是 15,因此只有在我们经过 de loop 15 次后才开始显示图片,它将进一步停止 15 个循环。

    在您给我们的以下循环中,如果您查看 在你的循环结束时你会看到 $number++。每次运行循环时,它都会将 $number 增加一。它只会显示之间的图像 15 和 15+15 (30)

    $number = 0;
    foreach ($files as $file){   
    if($number > $page  && $number < $page + 15){
    
    
    if ($file!="."&&$file!="..")
    {
    $extention = explode('.', $file);
    if ($extention[1] != "")
    {       
    echo "<div class='imgwrapper'>";
    echo"<a class='fancybox' rel='group' href='$dir$file' return='false' title='$filename'>";
    echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'>";
    echo"</a><br>";
    echo "</div>";
    }
    elseif($number > $page + 15)
    {
    // this stop the loop while you have showed your 15 pictures
    break;
    }
    
    
    $number++
    }
    }
    } 
    

    我没有尝试过,但我希望你能大致了解。

    【讨论】:

    • 你显然付出了很多努力,我很感激。不幸的是,这一切都在我身上迷失了。 :( 我不知道它去哪里以及它与什么交互(因为页面上显然有更多我没有包含的代码)。从我在上一页(MySQL)上编写代码的方式来看,它是简单得多,所以我想我认为它会是一个更小的 sn-p。
    • 好吧,如果你愿意,我们可以上Skype,但我不知道如何pm你。问题是你不去你的数据库来获取信息,所以你不能使用 LIMIT。您检索的信息来自您的文件夹,而不是您的数据库。
    • 不用担心 - 感谢您的帮助。我希望这将是一个比看起来更简单的任务,所以我必须想出别的办法。感谢您抽出宝贵时间回复和协助。
    【解决方案2】:

    您似乎已经完成了大部分工作,并且您已经在一个数组中存储了对文件的引用,因此您应该能够使用 slice 方法根据需要检索数组的一部分。伪代码给你思路:

    $total_to_display=10;
    $pn=$_GET['pn'];
    $preserve=true;
    
    $slice=array_slice( $files, $pn, $total_to_display, $preserve );
    
    foreach( $slice as $key => $file ) echo "<img src='$file' />";
    

    根据上述内容,这是一个经过测试的版本,它可以工作 - 尽管可能需要注意一些事情,因为它没有经过严格的测试!

            define('ROOT','c:/wwwroot' );
            $dir='/images/gallery/loch_clunie_April07';
            $fullpath=realpath( ROOT . $dir );
            $files=array();
    
            if( $fullpath ){
                /* Find jpg, png and gif images in nominate directory */
                $filepaths=preg_grep( '@\.jpg|\.png|\.gif@i', glob( $fullpath . '/*' ) );
    
                /* Prepare an array to hold data about each image in directory */
                foreach( $filepaths as $path ){
                    $info=(object)pathinfo( $path );
                    list( $width, $height, $type, $attr ) = getimagesize( $path );
                    $files[]=array( 'name'=>$info->basename, 'dir'=>$dir, 'ext'=>$info->extension, 'size'=>filesize( $path ), 'created'=>filectime( $path ), 'width'=>$width, 'height'=>$height );
                }
    
    
                /* Set default values to prevent errors */
                $tR=0;
                $mR=0;
                $tP=0;
                $pN=0;
    
                /* Number of images to display */
                $mR=15;
                /* Total number of images found in directory */
                $tR=count( $files );
                /* Current page number */
                $pN=isset( $_GET['pN'] ) ? filter_var( filter_input( INPUT_GET, 'pN', FILTER_SANITIZE_NUMBER_INT ), FILTER_VALIDATE_INT ) : 0;
                /* Determine number of pages */
                $tP=( $mR > 0 ) ? abs( ceil( $tR / $mR ) - 1 ) : 0;
    
    
                /* Ensure that the page number is valid based upon number of records to display AND number of images found */
                if( abs( $mR * $pN ) > $tR-1 ) $pN=0;
    
    
    
    
                $slice=array_slice( $files, abs( $pN * $mR ), $mR, false );
                foreach( $slice as $key => $array ){
                    $img=(object)$array;
                    echo "
                    <div class='imgwrapper'>
                        <a class='fancybox' rel='group' href='{$img->dir}{$img->name}' return='false' title='{$img->name}'>";
                        /* Replace the following with your timthumb code */
                    echo "
                            <img src='{$img->dir}/{$img->name}' />";
                    /*
                    echo "
                            <img src='timthumb.php?src={$img->dir}{img->name}&h={$img->height}&w={$img->width}' alt='{$img->ext}' width='{$img->width}' height='{$img->height}'>";
                    */
                    echo "
                        </a>
                    </div>";
                }
    
    
    
    
    
                /* Display pagination links - You might wish to prefix the actual links with the path to the gallery page rather than just ?pN=X etc */
                if( $tP > 0 && $tR > $mR ){
                    echo "<div class='rspaging'>";
    
                    if( $pN==0 ) echo "<div id='paging_first'>First</div>";
                    else echo "<a href='?pN=0'>First</a>";
    
                    if( $pN > 0 ) echo "<a href='?pN=".max( 0, $pN - 1 )."'>Previous</a>";
                    else echo "<div id='paging_previous'>Previous</div>";
    
                    if( ( $pN + 1 ) > $tP ) echo "<div id='paging_next'>Next</div>";
                    else echo "<a href='?pN=".min( $tP, $pN + 1 )."'>Next</a>";
    
                    if( $pN==$tP ) echo "<div id='paging_last'>Last</div>";
                    else echo "<a href='?pN={$tP}'>Last</a>";
    
                    echo "</div>";
                }
            }
    

    【讨论】:

    • 感谢您的回复。不幸的是,就像我上面的回答一样,我不知道把它放在哪里或它做了什么。我很认真地付钱请人做这件事。我只是对这段代码中检索图像的方法不够熟悉,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    相关资源
    最近更新 更多