我试图将分页添加到我当前的PHP库中,发现this是我迄今为止找到的最接近帮助的东西。我做了大量的阅读并得到了代码-kinda- working。
这是原始图库代码
$imageFolder = 'img/gallery/FaL/';
$pattern = '/[\w-]+. (jpg|png|gif|jpeg)/';
$i = 1; foreach(glob($imageFolder.'*') as $filename){
if(preg_match($pattern, $filename)) {
$filename = basename($filename);
if ($i % 6 == 1) echo '<div class="row">';
$image = '<div class="col-md-2">';
$image .= '<a href="'.$imageFolder.$filename.'" class="thumbnail">';
$image .= '<img src="'.$imageFolder.$filename.'" alt="" />';
$image .= '</a>';
$image .= '</div>';
echo $image;
if ($i % 6 == 0) echo '</div>';
$i++; } } if ($i % 6 != 1) echo '</div>';
以下是我目前对分页的看法。
$folder = 'img/gallery/FaL/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$total = count($files);
$per_page = 25;
$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;
}
//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'];
if ($i % 6 == 1) echo '<div class="row">';
$image = '<div class="col-md-2">';
$image .= '<a href="'.$folder.$filename.'.jpg" class="thumbnail">';
$image .= '<img src="'.$folder.$filename.'.jpg" alt="" />';
$image .= '</a>';
$image .= '</div>';
echo $image;
if ($i % 6 == 0) echo '</div>';
$i++;
}
if ($i % 6 != 1) echo '</div>';
show_pagination($page, $last_page);
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>';
}
我做了一个测试图库页面www.htopro.com/tg.php并且图片正常工作,我的fancybox工作正常,但我无法按照原始代码的方式正确布局 - www.htopro .COM / fal.php 我不确定我做错了什么,有人可以启发我吗?
答案 0 :(得分:0)
$imageFolder = $folder
echo '<div class="row">';
foreach(glob($imageFolder.'*') as $filename){
$filename = basename($filename);
$image = '<div class="col-md-2">';
$image .= '<a href="'.$imageFolder.$filename.'" class="thumbnail">';
$image .= '<img src="'.$imageFolder.$filename.'" alt="" />';
$image .= '</a>';
$image .= '</div>';
echo $image;
if ($i > 1 && $i % 6 == 0) echo '</div><div class="row">';
$i++;
}
echo "</div>";