2013-03-07 43 views
1

我有一個php頁面來顯示來自mysql數據庫的圖像。它會一頁接一頁地顯示上傳的所有圖像。我怎樣才能每頁顯示5或6圖像與分頁?從mysql分頁顯示圖像

這是我的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

你需要傳遞一個偏移量和限制你的'get_images()'函數,然後再處理它在你的數據庫中。請同時顯示'get_images()'函數。 – BenM 2013-03-07 10:01:27

+0

'get_images()'做了什麼? – 2013-03-07 10:01:30

+0

請開始嘗試相同的至少。編寫一些代碼分頁,以便我們能夠支持更多。 – 2013-03-07 10:06:14

回答

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; 

} 

認爲這將有助於你

+0

是的,謝謝。分頁適用於我的數據庫中的文本,但無法計算出顯示圖像。 – subhro 2013-03-07 10:15:27