从mysql分页显示图像

时间:2013-03-07 09:59:45

标签: php mysql pagination

我有一个php页面来显示mysql数据库中的图像。它会依次显示1页中上传的所有图像。如何以分页方式每页显示5或6张图像?

这是我的php页面。

<?Php

    include("init.php"); 
    include("template/header.php");


    ?>
    <div class="view_albums"><h3> View Albums </h3>;
    <?php



    $album_id = $_GET['album_id'];
    $images = get_images($album_id);

    if (empty($images)) {
        echo 'There are no images ';

    } else {


        foreach ($images as $image) {
            ?> <div class="box">  <?php
            echo '<a href="uploads/', $image['album'], '/', $image['id'], '.', $image['ext'],'"> <img class="box1" src="uploads/thumbs/', $image['album'], '/', $image['id'], '.', $image['ext'], '"  title="Uploaded on ', date('l F j, Y \a\t g:i A',$image['timestamp']),'"></a> [<a href="delete_image.php?image_id=">delete</a>]';
            ?>  <?php


        }
    }
    ?>
    </div>
    </div>
    <?php
    include("template/footer.php");

?>

1 个答案:

答案 0 :(得分:0)

尝试使用以下分页功能

function genPagination($total,$currentPage,$baseLink,$nextPrev=true,$limit=10) 
{ 
if(!$total OR !$currentPage OR !$baseLink) 
{ 
    return false; 
} 

//Total Number of pages 
$totalPages = ceil($total/$limit); 

//Text to use after number of pages 
$txtPagesAfter = ($totalPages==1)? " page": " pages"; 

//Start off the list. 
$txtPageList = '<br />'.$totalPages.$txtPagesAfter.' : <br />'; 

//Show only 3 pages before current page(so that we don't have too many pages) 
$min = ($page - 3 < $totalPages && $currentPage-3 > 0) ? $currentPage-3 : 1; 

//Show only 3 pages after current page(so that we don't have too many pages) 
$max = ($page + 3 > $totalPages) ? $totalPages : $currentPage+3; 

//Variable for the actual page links 
$pageLinks = ""; 

//Loop to generate the page links 
for($i=$min;$i<=$max;$i++) 
{ 
    if($currentPage==$i) 
    { 
        //Current Page 
        $pageLinks .= '<b class="selected">'.$i.'</b>'; 
    } 
    else 
    { 
        $pageLinks .= '<a href="'.$baseLink.$i.'" class="page">'.$i.'</a>'; 
    } 
} 

if($nextPrev) 
{ 
    //Next and previous links 
    $next = ($currentPage + 1 > $totalPages) ? false : '<a href="'.$baseLink.($currentPage + 1).'">Next</a>'; 

    $prev = ($currentPage - 1 <= 0 ) ? false : '<a href="'.$baseLink.($currentPage - 1).'">Previous</a>'; 
} 

return $txtPageList.$prev.$pageLinks.$next; 

}  

认为它会帮助你