Get All Photos From Folder and Paginate With PHP(从文件夹中获取所有照片并使用 PHP 进行分页)
问题描述
我正在尝试构建一个具有照片库的站点,而不是构建数据库 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).'"><<上一个</a>';}if( $current_page < $last_page ){echo ' <a href="?page='.($current_page+1).'">下一个>></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).'"> <<Previous </a> ';
    }
    if( $current_page < $last_page ){
        echo ' <a href="?page='.($current_page+1).'"> Next>> </a> ';  
    }
    echo '</div>';
}
?>
                        
这篇关于从文件夹中获取所有照片并使用 PHP 进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
	
	
	
		
			
				
				 沃梦达教程
				本文标题为:从文件夹中获取所有照片并使用 PHP 进行分页
				
				
            		
			
		
	
	
	
		        
            
				
        
	
	
				
            
 
            
        
	    	
	
								
	
		 基础教程推荐
	
	
		
		
		
		
		
		
		
		
		
		
		
	
 																	
	
	    
             猜你喜欢
        
	     
            - 
                PHPUnit 的 Selenium 2 文档到底在哪里?
                2022-01-01
            
 
- 
                Yii2 - 在运行时设置邮件传输参数
                2022-01-01
            
 
- 
                php中的PDF导出
                2022-01-01
            
 
- 
                如何在数学上评估像“2-1"这样的字符串?产生“1"?
                2022-01-01
            
 
- 
                主题化 Drupal 7 的 Ubercart “/cart"页
                2021-01-01
            
 
- 
                将变量从树枝传递给 js
                2022-01-01
            
 
- 
                php中的foreach复选框POST
                2021-01-01
            
 
- 
                Web 服务器如何处理请求?
                2021-01-01
            
 
- 
                使用 scandir() 在目录中查找文件夹 (PHP)
                2022-01-01
            
 
- 
                php 7.4 在写入变量中的 Twig 问题
                2022-01-01
            
 
        
	
 												
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				