doudi5892 2015-04-26 16:18
浏览 59

PHP - 从opendir数组创建分页(照片库页面)

I had done this using my MySQL db, but about a year ago, I revamped my site to a more conveniently-coding layout for my photography thumbnails. Someone else had written the code, and it uses the opendir functionality to auto-create thumbnail images based on the image files in a particular folder.

The problem is if I have a gallery with 100+ images in it, it loads all of them on the page and the user can't select anything to view until they've all loaded.

I want to create pagination to load, say, 15 at a time and then allow the user to go to the other pages (if there are any). This code was written way over my head, as I'm quite the PHP novice.

Here is the code I think is relevant that needs to be updated:

$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>";
}
}
}   
?>

That might not even be everything. Honestly, I'm willing to pay someone to do this for me. I don't know how hard or easy it is. I can send the entire page or copy the entire code if necessary.

I spent 7 hours yesterday trying to convert my site into a WordPress layout where there is a web gallery plugin that already does this, but the mobile layout is a mess and it's a little more complicated than what this code already does easily.
I think my current site looks and works great, but this pagination is the only thing missing.

  • 写回答

2条回答 默认 最新

  • douhu1990 2015-04-26 16:31
    关注

    You seem to have the bulk of the work done already and you have stored references to the files in an array so you should be able to use the slice method to retrieve portions of the array as you wish. Pseudo code to give you the idea:

    $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' />";
    

    Following on from the above, here is a tested version which works - though there may well be things that need attention as it has not been rigorously tested!

            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>";
                }
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来