从文件夹中获取所有照片并使用 PHP 进行分页

2022-01-04 00:00:00 php pagination

我正在尝试构建一个具有照片库的站点,而不是构建数据库 CMS,我正在尝试使用 PHP 和文件夹.目前我有一个脚本来获取文件夹中的所有图像并将它们显示在页面上,但是由于可能会超过 100 张照片,我想使用分页将其拆分为多个页面,但是我不知道该怎么做.

这是我目前正在运行的脚本:

';}?>

问题 1 - 如何提取没有文件扩展名的文件名?Q2 - 对于每页 24 张图片,我该如何分页?

解决方案

对于分页,您必须计算要分页的总项目数,捕获当前页面的参数并在相应范围内迭代.

$total){$max = $total;}

您可以使用pathinfo函数来获取不带扩展名的文件名.

//print_r($files);echo "正在处理的页面:$page 偏移量:$offset 最大值:$max 总计:$total last_page:$last_page";show_pagination($page, $last_page);for($i = $offset; $i< $max; $i++){$file = $files[$i];$path_parts = pathinfo($file);$filename = $path_parts['filename'];回声'<div class="galleryCellHolder"><div class="galleryCell"><a class="fancybox" rel="group" href="'.$file.'"><img class="galleryPhoto" src="'.$file.'" alt="'.$filename.'"></a>

';}show_pagination($page, $last_page);

使用以下功能可以创建导航链接

function show_pagination($current_page, $last_page){echo '

';如果( $current_page > 1 ){echo ' <a href="?page='.($current_page-1).'">&lt;&lt;上一个</a>';}if( $current_page < $last_page ){echo ' <a href="?page='.($current_page+1).'">下一个&gt;></a>';}回声'</div>';}?>

I'm trying to build a site that has a photo gallery and rather than build a database CMS I'm trying it with the use of PHP and folders. At the moment I have a script to get all of the images in a folder and display them on a page, however as there are going to be probably in excess of 100 photo's I'd like to use pagination to spllit this over several pages but I have no idea how to do this.

Here is the script I'm currently running:

<?php
$folder = 'cms/gallery/photo/';
$filetype = '*.*';
$filename = HOW DO I GET THE NAME WITHOUT FILE TYPE
$files = glob($folder.$filetype);
foreach ($files as $file)
{
    echo '

    <div class="galleryCellHolder">
        <div class="galleryCell">
            <a class="fancybox" rel="group" href="'.$file.'"><img class="galleryPhoto" src="'.$file.'" alt="'.$filename.'"></a>
        </div>
    </div>

    ';
}
?>

Q1 - How do I extract the file name without the file extension? Q2 - How do I paginate this for say 24 images per page?

解决方案

For paging you must calculate the total items to page , capture the parameter of the current page and iterate over the respective range.

<?php
$folder = 'cms/gallery/photo/';
$filetype = '*.*';    
$files = glob($folder.$filetype);    
$total = count($files);    
$per_page = 6;    
$last_page = (int)($total / $per_page);    
if(isset($_GET["page"])  && ($_GET["page"] <=$last_page) && ($_GET["page"] > 0) ){
    $page = $_GET["page"];
    $offset = ($per_page + 1)*($page - 1);      
}else{
    echo "Page out of range showing results for page one";
    $page=1;
    $offset=0;      
}    
$max = $offset + $per_page;    
if($max>$total){
    $max = $total;
}

You can use the function pathinfo to get the file name without extension.

    //print_r($files);
    echo "Processsing page : $page offset: $offset max: $max total: $total last_page: $last_page";        
    show_pagination($page, $last_page);        
    for($i = $offset; $i< $max; $i++){
        $file = $files[$i];
        $path_parts = pathinfo($file);
        $filename = $path_parts['filename'];        
        echo '        
        <div class="galleryCellHolder">
            <div class="galleryCell">
                <a class="fancybox" rel="group" href="'.$file.'"><img class="galleryPhoto" src="'.$file.'" alt="'.$filename.'"></a>
            </div>
        </div>        
        ';                
    }        
    show_pagination($page, $last_page);

Using the following function you can create the navigation links

function show_pagination($current_page, $last_page){
    echo '<div>';
    if( $current_page > 1 ){
        echo ' <a href="?page='.($current_page-1).'"> &lt;&lt;Previous </a> ';
    }
    if( $current_page < $last_page ){
        echo ' <a href="?page='.($current_page+1).'"> Next&gt;&gt; </a> ';  
    }
    echo '</div>';
}

?>

相关文章